-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_28.java
More file actions
65 lines (61 loc) · 2.05 KB
/
Copy pathP_28.java
File metadata and controls
65 lines (61 loc) · 2.05 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
62
63
64
65
package leetcode.easy;
public class P_28 {
// O(n+m)
public int strStr(String haystack, String needle) {
if (needle.isEmpty()) {
return 0;
}
final int[] prefixSuffix = kmp(needle);
for (int i = 0, j = 0; i < haystack.length() && j < needle.length(); i++) {
while (j > 0 && haystack.charAt(i) != needle.charAt(j)) {
j = prefixSuffix[j - 1];
}
if (haystack.charAt(i) == needle.charAt(j)) {
j++;
}
if (j == needle.length()) {
return i - j + 1;
}
}
return -1;
}
private static int[] kmp(String s) {
final int n = s.length();
final int[] prefixSuffix = new int[n];
for (int i = 1, j = 0; i < s.length(); i++) {
while (j > 0 && s.charAt(i) != s.charAt(j)) {
j = prefixSuffix[j - 1];
}
if (s.charAt(i) == s.charAt(j)) {
j++;
}
prefixSuffix[i] = j;
}
return prefixSuffix;
}
// Expected O(n+m), worst-case O(n*m)
public int strStrRabinKarp(String haystack, String needle) {
if (needle.length() > haystack.length()) {
return -1;
}
final int BASE = 26;
int tHash = 0, sHash = 0;
int powerS = 1;
for (int i = 0; i < needle.length(); i++) {
powerS = i > 0 ? powerS * BASE : 1;
tHash = tHash * BASE + haystack.charAt(i);
sHash = sHash * BASE + needle.charAt(i);
}
for (int i = needle.length(); i < haystack.length(); i++) {
if (tHash == sHash && haystack.startsWith(needle, i - needle.length())) {
return i - needle.length();
}
tHash -= haystack.charAt(i - needle.length()) * powerS;
tHash = tHash * BASE + haystack.charAt(i);
}
if (tHash == sHash && haystack.endsWith(needle)) {
return haystack.length() - needle.length();
}
return -1;
}
}