-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlongestpreffix.java
More file actions
59 lines (48 loc) · 1.38 KB
/
longestpreffix.java
File metadata and controls
59 lines (48 loc) · 1.38 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
package string;
public class longestpreffix {
static int longestPrefixSuffix(String s) {
int n = s.length();
int lps[] = new int[n];
// lps[0] is always 0
lps[0] = 0;
// length of the previous
// longest prefix suffix
int len = 0;
// the loop calculates lps[i]
// for i = 1 to n-1
int i = 1;
while (i < n) {
if (s.charAt(i) == s.charAt(len)) {
len++;
lps[i] = len;
i++;
}
// (pat[i] != pat[len])
else {
// This is tricky. Consider
// the example. AAACAAAA
// and i = 7. The idea is
// similar to search step.
if (len != 0) {
len = lps[len - 1];
// Also, note that we do
// not increment i here
}
// if (len == 0)
else {
lps[i] = 0;
i++;
}
}
}
int res = lps[n - 1];
// Since we are looking for
// non overlapping parts.
return (res > n / 2) ? n / 2 : res;
}
// Driver program
public static void main(String[] args) {
String s = "abcab";
System.out.println(longestPrefixSuffix(s));
}
}