Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions Insertion sort list
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <bits/stdc++.h>
using namespace std;

// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if (!head || !head->next) return head; // Base case

// Dummy node before the sorted part of the list
ListNode* dummy = new ListNode(0);
ListNode* curr = head;

while (curr) {
ListNode* nextNode = curr->next; // Save next node
ListNode* prev = dummy;

// Find the right place to insert current node
while (prev->next && prev->next->val < curr->val) {
prev = prev->next;
}

// Insert curr between prev and prev->next
curr->next = prev->next;
prev->next = curr;

// Move to the next node
curr = nextNode;
}

return dummy->next;
}
};

// Helper functions for testing
ListNode* createList(vector<int> vals) {
ListNode* head = nullptr;
ListNode* tail = nullptr;
for (int v : vals) {
if (!head) head = tail = new ListNode(v);
else {
tail->next = new ListNode(v);
tail = tail->next;
}
}
return head;
}

void printList(ListNode* head) {
while (head) {
cout << head->val;
if (head->next) cout << " -> ";
head = head->next;
}
cout << endl;
}

int main() {
Solution sol;

// Example test
vector<int> vals = {4, 2, 1, 3};
ListNode* head = createList(vals);

cout << "Original List: ";
printList(head);

head = sol.insertionSortList(head);

cout << "Sorted List: ";
printList(head);

return 0;
}
41 changes: 41 additions & 0 deletions Merge sorted arrays
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - 1; // pointer for nums1
int j = n - 1; // pointer for nums2
int k = m + n - 1; // pointer for merged position

// Merge from the end to avoid overwriting nums1
while (i >= 0 && j >= 0) {
if (nums1[i] > nums2[j]) {
nums1[k--] = nums1[i--];
} else {
nums1[k--] = nums2[j--];
}
}

// Copy remaining elements from nums2 (if any)
while (j >= 0) {
nums1[k--] = nums2[j--];
}
}
};

int main() {
Solution sol;

vector<int> nums1 = {1,2,3,0,0,0};
vector<int> nums2 = {2,5,6};
int m = 3, n = 3;

sol.merge(nums1, m, nums2, n);

cout << "Merged Array: ";
for (int num : nums1) cout << num << " ";
cout << endl;

return 0;
}
51 changes: 51 additions & 0 deletions Regular expression matching
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <bits/stdc++.h>
using namespace std;

bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));

dp[0][0] = true; // empty string matches empty pattern

// Handle patterns like a*, a*b*, a*b*c*
for (int j = 1; j <= n; j++) {
if (p[j - 1] == '*')
dp[0][j] = dp[0][j - 2];
}

// Fill the DP table
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (p[j - 1] == '.' || p[j - 1] == s[i - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
else if (p[j - 1] == '*') {
// Case 1: treat '*' as zero occurrence
dp[i][j] = dp[i][j - 2];

// Case 2: one or more occurrence
if (p[j - 2] == '.' || p[j - 2] == s[i - 1])
dp[i][j] = dp[i][j] || dp[i - 1][j];
}
}
}

return dp[m][n];
}

int main() {
vector<pair<string, string>> tests = {
{"aa", "a"},
{"aa", "a*"},
{"ab", ".*"},
{"mississippi", "mis*is*p*."},
{"aab", "c*a*b"}
};

for (auto &t : tests) {
cout << "s = \"" << t.first << "\", p = \"" << t.second << "\" → ";
cout << (isMatch(t.first, t.second) ? "true" : "false") << endl;
}

return 0;
}
34 changes: 34 additions & 0 deletions Shortest Palindrome
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <bits/stdc++.h>
using namespace std;

string shortestPalindrome(string s) {
string rev = s;
reverse(rev.begin(), rev.end());

string temp = s + "#" + rev;
vector<int> lps(temp.size(), 0);

// Build LPS array (same as KMP preprocessing)
for (int i = 1; i < temp.size(); i++) {
int j = lps[i - 1];
while (j > 0 && temp[i] != temp[j])
j = lps[j - 1];
if (temp[i] == temp[j])
j++;
lps[i] = j;
}

int palinLen = lps.back(); // Longest palindromic prefix length
string add = rev.substr(0, s.size() - palinLen);
return add + s;
}

int main() {
string s1 = "aacecaaa";
string s2 = "abcd";

cout << "Input: " << s1 << "\nOutput: " << shortestPalindrome(s1) << "\n\n";
cout << "Input: " << s2 << "\nOutput: " << shortestPalindrome(s2) << "\n";

return 0;
}
44 changes: 44 additions & 0 deletions String matching in an array
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <bits/stdc++.h>
using namespace std;

vector<string> stringMatching(vector<string>& words) {
vector<string> result;
int n = words.size();

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j && words[j].find(words[i]) != string::npos) {
result.push_back(words[i]);
break; // no need to check further once found
}
}
}

return result;
}

int main() {
vector<string> words1 = {"mass","as","hero","superhero"};
vector<string> words2 = {"leetcode","et","code"};
vector<string> words3 = {"blue","green","bu"};

auto printResult = [](vector<string> res) {
cout << "[";
for (int i = 0; i < res.size(); i++) {
cout << "\"" << res[i] << "\"";
if (i != res.size() - 1) cout << ",";
}
cout << "]\n";
};

cout << "Test 1: ";
printResult(stringMatching(words1));

cout << "Test 2: ";
printResult(stringMatching(words2));

cout << "Test 3: ";
printResult(stringMatching(words3));

return 0;
}
62 changes: 62 additions & 0 deletions The Skyline problem
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<pair<int, int>> heights;

// Split buildings into two events: start and end
for (auto& b : buildings) {
heights.push_back({b[0], -b[2]}); // start of building (negative height)
heights.push_back({b[1], b[2]}); // end of building (positive height)
}

// Sort events by x
// If x is same:
// - process starts before ends
// - higher buildings first for starts, lower buildings first for ends
sort(heights.begin(), heights.end());

multiset<int> activeHeights = {0}; // store current active building heights
int prevMax = 0;
vector<vector<int>> result;

for (auto& h : heights) {
int x = h.first, height = h.second;

if (height < 0) {
// Building start
activeHeights.insert(-height);
} else {
// Building end
activeHeights.erase(activeHeights.find(height));
}

int currMax = *activeHeights.rbegin();
if (currMax != prevMax) {
result.push_back({x, currMax});
prevMax = currMax;
}
}

return result;
}
};

// Example test
int main() {
Solution sol;
vector<vector<int>> buildings = {{2,9,10},{3,7,15},{5,12,12},{15,20,10},{19,24,8}};

vector<vector<int>> res = sol.getSkyline(buildings);

cout << "[";
for (auto& point : res) {
cout << "[" << point[0] << "," << point[1] << "]";
if (&point != &res.back()) cout << ",";
}
cout << "]" << endl;

return 0;
}