Skip to content

Commit 904d39c

Browse files
Create LongestCommonSubstring.java
1 parent 8129e51 commit 904d39c

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class LongestCommonSubstring {
2+
public int longestCommonSubstr(String s1, String s2) {
3+
4+
int n = s1.length();
5+
6+
int m = s2.length();
7+
8+
int[][] dp = new int[n+1][m+1];
9+
10+
for(int i = 0; i<n+1; i++){
11+
dp[i][0] = 0;
12+
}
13+
14+
for(int j = 0; j<m+1; j++){
15+
dp[0][j] = 0;
16+
}
17+
18+
int maxL = 0;
19+
20+
for(int i = 1; i<n+1; i++){
21+
for(int j = 1; j<m+1; j++){
22+
23+
if(s1.charAt(i-1) == s2.charAt(j-1)){
24+
dp[i][j] = 1+dp[i-1][j-1];
25+
maxL = Math.max(maxL, dp[i][j]);
26+
}
27+
else{
28+
dp[i][j] = 0;
29+
}
30+
}
31+
}
32+
33+
return maxL;
34+
35+
}
36+
}

0 commit comments

Comments
 (0)