-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMain.java
49 lines (45 loc) · 1.5 KB
/
Main.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
public class Main {
public static int[] getNext(String pattern) {
int[] next = new int[pattern.length()];
next[0] = 0;
int index = 0;
for (int i = 1; i < pattern.length();) {
if (pattern.charAt(index) == pattern.charAt(i)) {
next[i] = index + 1;
++i;
++index;
} else {
if (index != 0) {
index = next[index - 1];
} else {
next[i] = 0;
++i;
}
}
}
return next;
}
public static Integer getIndex(String text, String pattern) {
int[] next = getNext(pattern);
int t = 0, p = 0;
while (t < text.length() && p < pattern.length()) {
if (text.charAt(t) == pattern.charAt(p)) {
++t;
++p;
} else {
if (p != 0) p = next[p - 1];
else t++;
}
}
return p == pattern.length() ? t - p : -1;
}
public static Boolean hasSubstring(String text, String pattern) {
return getIndex(text, pattern) != -1;
}
public static void main(String[] args) {
String text = "abcabaaabaabcac";
String pattern = "abaabcac";
System.out.println("Does the `text` contain the `pattern`? " + hasSubstring(text , pattern));
System.out.println("What's the index that the `pattern` in the `text`? " + getIndex(text, pattern));
}
}