Skip to content

Commit 11d0e51

Browse files
Merge pull request #509 from pranava7/main
LCSubstring solution added
2 parents ad8b78b + 4c178cf commit 11d0e51

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// largest contiguous array sum
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
int maxSubArraySum(int a[], int size) {
7+
int max_so_far = INT_MIN, max_ending_here = 0;
8+
9+
for (int i = 0; i < size; i++) {
10+
max_ending_here = max_ending_here + a[i];
11+
if (max_so_far < max_ending_here)
12+
max_so_far = max_ending_here;
13+
14+
if (max_ending_here < 0)
15+
max_ending_here = 0;
16+
}
17+
return max_so_far;
18+
}
19+
20+
int main() {
21+
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
22+
int n = sizeof(a) / sizeof(a[0]);
23+
24+
int max_sum = maxSubArraySum(a, n);
25+
cout << "Maximum contiguous sum is " << max_sum;
26+
return 0;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Dynamic Programming solution to find length of the longest common substring*/
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
int LongestCommonSubStr(string X, string Y, int m, int n) {
7+
8+
int dp[m + 1][n + 1];
9+
int result = 0;
10+
11+
for (int i = 0; i <= m; i++) {
12+
for (int j = 0; j <= n; j++) {
13+
if (i == 0 || j == 0)
14+
dp[i][j] = 0;
15+
16+
else if (X[i - 1] == Y[j - 1]) {
17+
dp[i][j] = dp[i - 1][j - 1] + 1;
18+
result = max(result, dp[i][j]);
19+
} else
20+
dp[i][j] = 0;
21+
}
22+
}
23+
return result;
24+
}
25+
26+
int main() {
27+
string X = "FirstCommit";
28+
string Y = "LastCommit";
29+
30+
int m = X.length();
31+
int n = Y.length();
32+
33+
cout << "Length of Longest Common Substring is " << LongestCommonSubStr(X, Y, m, n);
34+
return 0;
35+
}

0 commit comments

Comments
 (0)