diff --git a/Matrix/MaxSizeRectangle.cpp b/Matrix/MaxSizeRectangle.cpp new file mode 100644 index 0000000..7fe95c2 --- /dev/null +++ b/Matrix/MaxSizeRectangle.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +// Rows and columns in input matrix +#define R 4 +#define C 4 + +// Finds the maximum area under +// the histogram represented +// by histogram. See below article for details. + + +int maxHist(int row[]) +{ + // Create an empty stack. + // The stack holds indexes of + // hist[] array/ The bars stored + // in stack are always + // in increasing order of their heights. + stack result; + + int top_val; // Top of stack + + int max_area = 0; // Initialize max area in current + // row (or histogram) + + int area = 0; // Initialize area with current top + + // Run through all bars of given histogram (or row) + int i = 0; + while (i < C) { + // If this bar is higher than the bar on top stack, + // push it to stack + if (result.empty() || row[result.top()] <= row[i]) + result.push(i++); + + else { + // If this bar is lower than top of stack, then + // calculate area of rectangle with stack top as + // the smallest (or minimum height) bar. 'i' is + // 'right index' for the top and element before + // top in stack is 'left index' + top_val = row[result.top()]; + result.pop(); + area = top_val * i; + + if (!result.empty()) + area = top_val * (i - result.top() - 1); + max_area = max(area, max_area); + } + } + + // Now pop the remaining bars from stack and calculate + // area with every popped bar as the smallest bar + while (!result.empty()) { + top_val = row[result.top()]; + result.pop(); + area = top_val * i; + if (!result.empty()) + area = top_val * (i - result.top() - 1); + + max_area = max(area, max_area); + } + return max_area; +} + +// Returns area of the largest rectangle with all 1s in +// A[][] +int maxRectangle(int A[][C]) +{ + // Calculate area for first row and initialize it as + // result + int result = maxHist(A[0]); + + // iterate over row to find maximum rectangular area + // considering each row as histogram + for (int i = 1; i < R; i++) { + + for (int j = 0; j < C; j++) + + // if A[i][j] is 1 then add A[i -1][j] + if (A[i][j]) + A[i][j] += A[i - 1][j]; + + // Update result if area with current row (as last + // row) of rectangle) is more + result = max(result, maxHist(A[i])); + } + + return result; +} + +// Driver code +int main() +{ + int A[][C] = { + { 0, 1, 1, 0 }, + { 1, 1, 1, 1 }, + { 1, 1, 1, 1 }, + { 1, 1, 0, 0 }, + }; + + cout << "Area of maximum rectangle is " + << maxRectangle(A); + + return 0; +} \ No newline at end of file diff --git a/Matrix/spiralMatrix.cpp b/Matrix/spiralMatrix.cpp new file mode 100644 index 0000000..89e0337 --- /dev/null +++ b/Matrix/spiralMatrix.cpp @@ -0,0 +1,47 @@ +class Solution { +public: + vector spiralOrder(vector>& matrix) { + // Calculate the total number of rows and columns + int rows = matrix.size(); + int cols = matrix[0].size(); + + // Set up pointers to traverse the matrix + int row = 0; + int col = -1; + + // Set the initial direction to 1 for moving left to right + int direction = 1; + + // Create an array to store the elements in spiral order + vector result; + + // Traverse the matrix in a spiral order + while (rows > 0 && cols > 0) { + + // Move horizontally in one of two directions: + // 1. Left to right (if direction == 1) + // 2. Right to left (if direction == -1) + // Increment the col pointer to move horizontally + for (int i = 0; i < cols; i++) { + col += direction; + result.push_back(matrix[row][col]); + } + rows--; + + // Move vertically in one of two directions: + // 1. Top to bottom (if direction == 1) + // 2. Bottom to top (if direction == -1) + // Increment the row pointer to move vertically + for (int i = 0; i < rows; i++) { + row += direction; + result.push_back(matrix[row][col]); + } + cols--; + + // Flip the direction for the next traversal + direction *= -1; + } + + return result; + } +}; \ No newline at end of file diff --git a/String/BoyerMoore.cpp b/String/BoyerMoore.cpp new file mode 100644 index 0000000..4e365e0 --- /dev/null +++ b/String/BoyerMoore.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; +#define NO_OF_CHARS 256 + +// The preprocessing function for Boyer Moore's +// bad character heuristic +void badCharHeuristic(string str, int size, + int badchar[NO_OF_CHARS]) +{ + int i; + + // Initialize all occurrences as -1 + for (i = 0; i < NO_OF_CHARS; i++) + badchar[i] = -1; + + // Fill the actual value of last occurrence + // of a character + for (i = 0; i < size; i++) + badchar[(int)str[i]] = i; +} + +/* A pattern searching function that uses Bad +Character Heuristic of Boyer Moore Algorithm */ +void search(string txt, string pat) +{ + int m = pat.size(); + int n = txt.size(); + + int badchar[NO_OF_CHARS]; + + /* Fill the bad character array by calling + the preprocessing function badCharHeuristic() + for given pattern */ + badCharHeuristic(pat, m, badchar); + + int s = 0; // s is shift of the pattern with + // respect to text + while (s <= (n - m)) { + int j = m - 1; + + /* Keep reducing index j of pattern while + characters of pattern and text are + matching at this shift s */ + while (j >= 0 && pat[j] == txt[s + j]) + j--; + + /* If the pattern is present at current + shift, then index j will become -1 after + the above loop */ + if (j < 0) { + cout << "pattern occurs at shift = " << s + << endl; + + /* Shift the pattern so that the next + character in text aligns with the last + occurrence of it in pattern. + The condition s+m < n is necessary for + the case when pattern occurs at the end + of text */ + s += (s + m < n) ? m - badchar[txt[s + m]] : 1; + } + + else + /* Shift the pattern so that the bad character + in text aligns with the last occurrence of + it in pattern. The max function is used to + make sure that we get a positive shift. + We may get a negative shift if the last + occurrence of bad character in pattern + is on the right side of the current + character. */ + s += max(1, j - badchar[txt[s + j]]); + } +} + +/* Driver code */ +int main() +{ + string txt = "ABAAABCD"; + string pat = "ABC"; + search(txt, pat); + return 0; +} + \ No newline at end of file diff --git a/String/DistinctSubsequences.cpp b/String/DistinctSubsequences.cpp new file mode 100644 index 0000000..f0b4018 --- /dev/null +++ b/String/DistinctSubsequences.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int dp[1001][1001]; + int solve(int currentInd, int currentTarIn, string &s, string &t) { + if(currentTarIn >= (t.size())) { + return 1; + } + if(currentInd>=(s.size())) { + return 0; + } + if(dp[currentInd][currentTarIn] != -1) { + return dp[currentInd][currentTarIn]; + } + int res1 = solve(currentInd + 1, currentTarIn, s, t); + if(s[currentInd] == t[currentTarIn]) { + res1 += solve(currentInd + 1, currentTarIn + 1, s, t); + } + return dp[currentInd][currentTarIn] = res1; + } + int numDistinct(string s, string t) { + memset(dp, -1, sizeof(dp)); + return solve(0, 0, s, t); + } +}; \ No newline at end of file diff --git a/String/LargestREctangle.cpp b/String/LargestREctangle.cpp new file mode 100644 index 0000000..8b2a839 --- /dev/null +++ b/String/LargestREctangle.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int largestRectangleArea(vector& heights) { + std::stack stack; + stack.push(-1); + int max_area = 0; + + for (int i = 0; i < heights.size(); i++) { + while (stack.top() != -1 && heights[i] <= heights[stack.top()]) { + int height = heights[stack.top()]; + stack.pop(); + int width = i - stack.top() - 1; + max_area = std::max(max_area, height * width); + } + stack.push(i); + } + + while (stack.top() != -1) { + int height = heights[stack.top()]; + stack.pop(); + int width = heights.size() - stack.top() - 1; + max_area = std::max(max_area, height * width); + } + + return max_area; + } +}; \ No newline at end of file diff --git a/String/MinimumWindow.cpp b/String/MinimumWindow.cpp new file mode 100644 index 0000000..48e6698 --- /dev/null +++ b/String/MinimumWindow.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + string minWindow(string s, string t) { + if (s.length() < t.length()) return ""; + + vector map(128, 0); + int count = t.length(); + int start = 0, minStart = 0, minLen = INT_MAX; + + for (char c : t) map[c]++; + + for (int end = 0; end < s.length(); end++) { + if (map[s[end]]-- > 0) count--; + + while (count == 0) { + if (end - start + 1 < minLen) { + minStart = start; + minLen = end - start + 1; + } + + if (map[s[start++]]++ == 0) count++; + } + } + + return minLen == INT_MAX ? "" : s.substr(minStart, minLen); + } +}; \ No newline at end of file diff --git a/String/TextJustification.cpp b/String/TextJustification.cpp new file mode 100644 index 0000000..e17dd5d --- /dev/null +++ b/String/TextJustification.cpp @@ -0,0 +1,66 @@ +class Solution { +public: + vector fullJustify(vector& words, int maxWidth) { + int nows = 0; + int newLen = 0; + int mainLen = words.size(); + vector updatedStrings; + for(int i = 0; i < mainLen; i++){ + newLen = newLen + words[i].size(); + nows++; + + if(newLen + (nows-1) <= maxWidth){ + if(i == (mainLen - 1)){ + string s = ""; + for(int k = 0; k < nows; k++){ + s += words[i - (nows - 1) + k]; + if(k < (nows-1)) s += " "; + } + for(int l = s.size(); l < maxWidth; l++) + s += " "; + + updatedStrings.push_back(s); + break; + } + else + continue; + } + else{ + nows--; + int spaces = maxWidth - (newLen - words[i].size()); + int ws = spaces, extra=0; + string s1 = ""; + string s2 = ""; + if(nows > 1){ + ws = spaces/(nows - 1); + extra = spaces%(nows - 1); + } + for(int k = 0; k < ws; k++){ + s2 += " "; + } + if(nows == 1){ + s1 += words[i - 1]; + s1 += s2; + } + else{ + for(int j = 0; j < nows - 1; j++){ + s1 += words[i - nows + j]; + s1 += s2; + if(extra){ + s1 += " "; + extra--; + } + } + s1 += words[i - 1]; + } + + updatedStrings.push_back(s1); + newLen = 0; + nows = 0; + + } + i -= 1; + } + return updatedStrings; + } +}; \ No newline at end of file