-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-19-LongestDuplicateSubstring.java
43 lines (37 loc) · 1.2 KB
/
06-19-LongestDuplicateSubstring.java
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
class Solution {
String str;
long mod = (long) Math.pow(2, 32);
int len;
public String longestDupSubstring(String s) {
len = s.length();
if (len == 0) return "";
str = s;
int lo = 1, hi = len - 1;
String ans = "";
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
String su = sub(mid);
if (su != "") {
ans = su;
lo = mid + 1;
} else hi = mid - 1;
}
return ans;
}
public String sub(int id) {
Set<Long> set = new HashSet<>();
long temp = 0;
for (int i = 0; i < id; i++) temp = (temp * 26 + str.charAt(i) - 'a') % mod;
set.add(temp);
long aL = 1;
for (int i = 1; i <= id; i++) aL = (aL * 26) % mod;
for (int j = 1; j < len + 1 - id; j++) {
temp = (temp * 26 - ((str.charAt(j - 1) - 'a')) * aL % mod + mod) % mod;
temp = (temp + (str.charAt(j + id - 1) - 'a')) % mod;
// System.out.println("id:" + id + " sub:" + str.substring(j, j+id));
if (set.contains(temp)) return str.substring(j, j + id);
set.add(temp);
}
return "";
}
}