-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternMatching.java
More file actions
61 lines (53 loc) · 1.54 KB
/
PatternMatching.java
File metadata and controls
61 lines (53 loc) · 1.54 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
/**
* Leetcode problem #28, Find the Index of the First Occurrence in a String
* https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/
* Using KMP pattern searching
*/
public class PatternMatching {
public static void main(String[] args) {
String haystack = "ABABDABACDABABCABAB";
String needle = "ABABCABAB";
int index = strStr(haystack, needle);
System.out.println(index);
}
public static int strStr(String haystack, String needle) {
int[] lps = computeLps(needle);
int i = 0, j = 0;
while (i < haystack.length()) {
if (haystack.charAt(i) == needle.charAt(j)) {
i++;
j++;
} else {
if (j == 0) {
i++;
} else {
j = lps[j - 1];
}
}
if (j == needle.length()) {
return i - j;
}
}
return -1;
}
public static int[] computeLps(String s) {
int[] lps = new int[s.length()];
int prevLps = 0, i = 1;
lps[0] = 0;
while (i < s.length()) {
if (s.charAt(i) == s.charAt(prevLps)) {
prevLps++;
lps[i] = prevLps;
i++;
} else {
if (prevLps == 0) {
lps[i] = 0;
i++;
} else {
prevLps = lps[prevLps - 1];
}
}
}
return lps;
}
}