-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathKMP.java
More file actions
65 lines (60 loc) · 2.18 KB
/
KMP.java
File metadata and controls
65 lines (60 loc) · 2.18 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
public class KMP {
static int generateLPS(String str, int stringLen) {
// Suffix value get
for (int len = stringLen - 1; len > 0; len--) {
// Prefix Value Get
boolean isMatch = true;
for (int i = 0; i < len; i++) {
System.out.println("I Prefix " + i + " Prefix Char " + str.charAt(i) + " Suffix "
+ (stringLen - len + i) + " Suffix Char " + str.charAt(stringLen - len + i));
if (str.charAt(i) != str.charAt(stringLen - len + i)) {
isMatch = false;
}
}
if (isMatch) {
return len;
}
}
// No Char Match return 0 length
return 0; // return length of Common Char Prefix , Suffix
}
static void better(String str, int[] lps) {
int strLen = str.length();
int len = 0;
lps[0] = 0; // first prefix is always prefix , so prefix and suffix not match so length is 0
int i = 1;// start from 1st index (2nd Char)
while (i < strLen) {
char prefix = str.charAt(len); // start 0
char suffix = str.charAt(i); // next to the length
if (prefix == suffix) {
len++;
lps[i] = len;
i++; // move to the next char
} else {
// prefix not match with suffix
if (len == 0) {
// first char
lps[i] = 0; // set 0 length
i++; // move to the next character
} else {
len = lps[len - 1]; // move the previous index of lps array which consist of last matching prefix
// length
}
}
}
}
public static void main(String[] args) {
String str = "ababc";
int lps[] = new int[str.length()];
// Old Code
// for (int i = 0; i < lps.length; i++) {
// lps[i] = generateLPS(str, i + 1);
// }
// New Code
better(str, lps);
for (int e : lps) {
System.out.print(e + " ");
}
System.out.println();
}
}