-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRS.java
More file actions
31 lines (29 loc) · 924 Bytes
/
Copy pathLRS.java
File metadata and controls
31 lines (29 loc) · 924 Bytes
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
// longest repeated substring
public class LRS {
public static String lcp(String s1, String s2) {
int min = Math.min(s1.length(), s2.length());
for (int i = 0; i < min; i++) {
if (s1.charAt(i) != s2.charAt(i))
return s1.substring(0, i);
}
return s1.substring(0, min);
}
public static String lrs(String string) {
int l = string.length();
String[] suffixes = new String[l];
for (int i = 0; i < l; i++) {
suffixes[i] = string.substring(i, l);
}
Sort.mergeSort(suffixes);
String lrs = "";
for (int i = 0; i < l - 1; i++) {
String x = lcp(suffixes[i], suffixes[i + 1]);
if (x.length() > lrs.length()) lrs = x;
}
return lrs;
}
public static void main(String[] args) {
String s = args[0];
System.out.println(lrs(s));
}
}