-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLongestSubString.java
More file actions
47 lines (38 loc) · 1.07 KB
/
LongestSubString.java
File metadata and controls
47 lines (38 loc) · 1.07 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
40
41
42
43
44
45
46
47
package com.cyn.Coursera;
/**
* @author Cui Yanna on 2019/4/8
*/
public class LongestSubString {
private String a;
private String b;
private int[][] f;
private int result;
public LongestSubString(String a, String b) {
this.a = a;
this.b = b;
}
public static void main(String[] args) {
LongestSubString obj = new LongestSubString("abc", "abcdabc");
System.out.println(obj.function());
}
private int function() {
int lenA = a.length();
int lenB = b.length();
f = new int[lenA + 1][lenB + 1];
for (int i = 0; i <= lenA; i++) {
f[i][0] = 0;
}
for (int i = 0; i <= lenB; i++) {
f[0][i] = 0;
}
for (int i = 0; i < lenA; i++)
for (int j = 0; j < lenB; j++) {
if(a.charAt(i) == b.charAt(j))
f[i + 1][j + 1] = f[i][j] + 1;
else
f[i + 1][j + 1] = 0;
result = Math.max(result, f[i + 1][j + 1]);
}
return result;
}
}