-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathCode03_MatchCount.java
66 lines (61 loc) · 1.36 KB
/
Code03_MatchCount.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package class36;
// 来自美团
// 给定两个字符串s1和s2
// 返回在s1中有多少个子串等于s2
public class Code03_MatchCount {
public static int sa(String s1, String s2) {
if (s1 == null || s2 == null || s1.length() < s2.length()) {
return 0;
}
char[] str1 = s1.toCharArray();
char[] str2 = s2.toCharArray();
return count(str1, str2);
}
// 改写kmp为这道题需要的功能
public static int count(char[] str1, char[] str2) {
int x = 0;
int y = 0;
int count = 0;
int[] next = getNextArray(str2);
while (x < str1.length) {
if (str1[x] == str2[y]) {
x++;
y++;
if (y == str2.length) {
count++;
y = next[y];
}
} else if (next[y] == -1) {
x++;
} else {
y = next[y];
}
}
return count;
}
// next数组多求一位
// 比如:str2 = aaaa
// 那么,next = -1,0,1,2,3
// 最后一个3表示,终止位置之前的字符串最长前缀和最长后缀的匹配长度
// 也就是next数组补一位
public static int[] getNextArray(char[] str2) {
if (str2.length == 1) {
return new int[] { -1, 0 };
}
int[] next = new int[str2.length + 1];
next[0] = -1;
next[1] = 0;
int i = 2;
int cn = 0;
while (i < next.length) {
if (str2[i - 1] == str2[cn]) {
next[i++] = ++cn;
} else if (cn > 0) {
cn = next[cn];
} else {
next[i++] = 0;
}
}
return next;
}
}