- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubstring.java
More file actions
Latest commit
31 lines (25 loc) · 704 Bytes
/
Copy pathLongestCommonSubstring.java
File metadata and controls
31 lines (25 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
packagedynamicProgramming;
/*
Algorithm:Pair chars of str2 with str1 (increasing window of length for both). Refer the memo matrix.
1. If same char then 1 + memo[i-1][j-1]
*/
publicclassLongestCommonSubstring {
publicstaticintlcs(Strings1, Strings2) {
int[][] memo = newint[s1.length() + 1][s2.length() + 1];
intmax = 0;
for (inti = 1; i <= s1.length(); i++) {
for (intj = 1; j <= s2.length(); j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
memo[i][j] = 1 + memo[i - 1][j - 1];
}
if (memo[i][j] > max) {
max = memo[i][j];
}
}
}
returnmax;
}
publicstaticvoidmain(String[] args) {
System.out.println(lcs("abcdaf", "zbcdf"));
}
}