-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementStr.java
More file actions
61 lines (60 loc) · 1.46 KB
/
ImplementStr.java
File metadata and controls
61 lines (60 loc) · 1.46 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
59
60
61
/********************************************************
> File Name:28 ImplementStr.java
> Auther: ihochang
> Mail: yihez@andrew.cmu.edu
> Created Time: Fri Jan 15 11:54:30 2016
*********************************************************/
public class ImplementStr {
public int strStr(String haystack, String needle) {
if (haystack == null || needle == null)
return 0;
int h = haystack.length();
int n = needle.length();
if (n>h)
return -1;
if(n == 0)
return 0;
int[] next = getNext(needle);
int i = 0;
while (i <= h-n) {
int success = 1;
for (int j= 0; j<n;j++) {
if (needle.charAt(0) != haystack.charAt(i)) {
success = 0;
i++;
break;
} else if (needle.charAt(j) != haystack.charAt(i+j)) {
success = 0;
i = i + j - next[j-1];
break;
}
}
if (success == 1)
return i;
}
return -1;
}
//calculate KMP array
public int[] getNext(String needle) {
int[] next = new int[needle.length()];
next[0] = 0;
for (int i = 1;i<needle.length();i++) {
int index = next[i-1];
while (index > 0 && needle.charAt(index) != needle.charAt(i)) {
index = next[index-1];
}
if (needle.charAt(index) == needle.charAt(i)) {
next[i] = next[i-1] + 1;
} else {
next[i] = 0;
}
}
return next;
}
public static void main(String[] args) {
String haystack = "abababac";
String test = "bac";
ImplementStr so = new ImplementStr();
System.out.println(so.strStr(haystack,test));
}
}