-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubstring.java
More file actions
39 lines (36 loc) · 1.16 KB
/
Copy pathLongestCommonSubstring.java
File metadata and controls
39 lines (36 loc) · 1.16 KB
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
32
33
34
35
36
37
38
39
package dp;
import org.junit.Test;
/**
* @Author: wei1
* @Date: Create in 2018/12/10 15:44
* @Description: 最大公共子串
* c[i][j]=0; i=0||j==0
* c[i][j]=c[i-1][j-1]+1 i>0&&j>0&&xi==yj
* c[i][j]=0 i>0&&j>0&&xi!=yj
*/
public class LongestCommonSubstring {
public int longestCommonSubstring(char[] arr1, char[] arr2) {
if (arr1 == null || arr2 == null || arr1.length == 0 || arr2.length == 0) {
return 0;
}
int[][] dp = new int[arr1.length + 1][arr2.length + 1];
int max = 0;
for (int i = 1; i < arr1.length + 1; i++) {
for (int j = 1; j < arr2.length + 1; j++) {
if (arr1[i - 1] == arr2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
max = Math.max(dp[i][j], max);
} else {
dp[i][j] = 0;
}
}
}
return max;
}
@Test
public void test() {
String s1 = "ABCBDAB";
String s2 = "BDCABCBD";
System.out.println(longestCommonSubstring(s1.toCharArray(), s2.toCharArray()));
}
}