-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestRepeatedSubstring.java
More file actions
39 lines (30 loc) · 1.03 KB
/
LongestRepeatedSubstring.java
File metadata and controls
39 lines (30 loc) · 1.03 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
package string;
public class LongestRepeatedSubstring {
public static String lrs(String s) {
int N = s.length();
String[] suffixes = new String[N];
for (int i = 0; i < N; i++)
suffixes[i] = s.substring(i, N);
ThreeWayStringQuicksort.sort(suffixes);
String lrs = "";
for (int i = 0; i < N - 1; i++) {
int len = longestCommonPrefix(suffixes[i], suffixes[i + 1]);
if (len > lrs.length())
lrs = suffixes[i].substring(0, len);
}
return lrs;
}
private static int longestCommonPrefix(String v, String w) {
int min = v.length() < w.length() ? v.length() : w.length();
int count = 0;
for (int i = 0; i < min; i++) {
if (v.charAt(i) != w.charAt(i)) return count;
count++;
}
return count;
}
public static void main(String[] args) {
String s = "aacaagtttacaagc";
System.out.println("The longest common substring is: " + lrs(s));
}
}