-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMPAlgorithm.java
More file actions
65 lines (55 loc) · 1.77 KB
/
KMPAlgorithm.java
File metadata and controls
65 lines (55 loc) · 1.77 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 String;
import java.util.ArrayList;
public class KMPAlgorithm {
// Function to compute the Longest Proper Prefix which is also Suffix (LPS) array.
public static void computeLPSArray(String pat, int M, int[] lps) {
int len = 0; // Length of the previous longest prefix suffix.
lps[0] = 0; // lps[0] is always 0.
int i = 1;
// Loop to compute the LPS array.
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
// Function to search for the pattern in the given text.
private static ArrayList<Integer> search(String pat, String txt) {
ArrayList<Integer> res = new ArrayList<>();
int M = pat.length();
int N = txt.length();
int[] lps = new int[M + 1];
computeLPSArray(pat, M, lps);
int j = 0; // Index for pattern.
int f = 0; // Flag to track if the pattern was found.
int i = 0; // Index for text.
// Loop to search for the pattern in the text.
while (i < N) {
if (pat.charAt(j) == txt.charAt(i)) {
j++;
i++;
}
if (j == M) {
f++;
res.add(i - j + 1);
j = lps[j - 1];
} else if (i < N && pat.charAt(j) != txt.charAt(i)) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
if (f == 0) res.add(-1);
return res;
}
}