-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_28.java
More file actions
59 lines (53 loc) · 1.26 KB
/
Solution_28.java
File metadata and controls
59 lines (53 loc) · 1.26 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
48
49
50
51
52
53
54
55
56
57
58
package com.hilbert25.leetcode;
/**
* @author : hilbert25
* @version 创建时间:2017年3月3日 下午5:08:33 LeetCode com.hilbert25.leetcode Solution_28
*/
public class Solution_28 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1 = "abaabcac";
String str2 = "aab";
System.out.println(kmp(str1, str2));
System.out.println(str1.indexOf(str2));
}
public static int[] next(String str) {
int[] nextArr = new int[str.length()];
nextArr[0] = 0;
for (int i = 1; i < str.length(); i++) {
nextArr[i] = 1;
int n = i - 1;
while (n >= 1) {
if (str.substring(0, n).equals(str.substring(i - n, i))) {
nextArr[i] = n + 1;
break;
}
n--;
}
}
return nextArr;
}
public static int kmp(String haystack, String needle) {
if(needle.equals(haystack))
return 0;
if (needle.length() == 0||haystack.length()==0)
return -1;
int i = 0, j = 0;
int[] nextArr = next(needle);
while (j + i < haystack.length()) {
if (haystack.charAt(i + j) == needle.charAt(i)) {
i++;
} else {
if (i == 0) {
j++;
} else {
i = nextArr[i] > 0 ? 0 : i - 1;
j = j + i - (nextArr[i] - 1);
}
}
if (i == needle.length())
return j;
}
return -1;
}
}