Skip to content

Commit 4669360

Browse files
authored
feat: update solutions to lc problem: No.1044
No.1044.Longest Duplicate Substring
1 parent e419d0a commit 4669360

File tree

8 files changed

+50
-51
lines changed

8 files changed

+50
-51
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
- [单词规律 II](/solution/0200-0299/0291.Word%20Pattern%20II/README.md) - `哈希表``回溯`
6464
- [最短回文串](/solution/0200-0299/0214.Shortest%20Palindrome/README.md) - `字符串哈希`
6565
- [回文对](/solution/0300-0399/0336.Palindrome%20Pairs/README.md) - `字符串哈希`
66+
- [最长重复子串](/solution/1000-1099/1044.Longest%20Duplicate%20Substring/README.md) - `字符串哈希` - `二分查找`
6667

6768
### 3. 搜索
6869

README_EN.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
6060
- [Constrained Subsequence Sum](/solution/1400-1499/1425.Constrained%20Subsequence%20Sum/README_EN.md) - `Dynamic Programming`, `Monotonic Queue`
6161
- [Word Pattern II](/solution/0200-0299/0291.Word%20Pattern%20II/README_EN.md) - `Hash Table``Backtracking`
6262
- [Shortest Palindrome](/solution/0200-0299/0214.Shortest%20Palindrome/README_EN.md) - `Rabin-Karp`
63-
- [Palindrome Pairs](/solution/0300-0399/0336.Palindrome%20Pairs/README_EN.md)) - `Rabin-Karp`
63+
- [Palindrome Pairs](/solution/0300-0399/0336.Palindrome%20Pairs/README_EN.md) - `Rabin-Karp`
64+
- [Longest Duplicate Substring](/solution/1000-1099/1044.Longest%20Duplicate%20Substring/README_EN.md) - `Rabin-Karp`, `Binary search`
6465

6566
### 3. Search
6667

solution/1000-1099/1044.Longest Duplicate Substring/README.md

+16-17
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42-
字符串哈希 + 二分查找
42+
**方法一:字符串哈希 + 二分查找**
4343

4444
字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。二分枚举长度,找到满足条件的最大长度即可。
4545

@@ -52,24 +52,23 @@
5252
```python
5353
class Solution:
5454
def longestDupSubstring(self, s: str) -> str:
55-
n = len(s)
56-
5755
def check(l):
58-
seen = set()
56+
vis = set()
5957
for i in range(n - l + 1):
6058
t = s[i: i + l]
61-
if t in seen:
59+
if t in vis:
6260
return t
63-
seen.add(t)
61+
vis.add(t)
6462
return ''
6563

64+
n = len(s)
6665
left, right = 0, n
6766
ans = ''
6867
while left < right:
6968
mid = (left + right + 1) >> 1
7069
t = check(mid)
7170
ans = t or ans
72-
if len(t) > 0:
71+
if t:
7372
left = mid
7473
else:
7574
right = mid - 1
@@ -112,14 +111,14 @@ class Solution {
112111

113112
private String check(String s, int len) {
114113
int n = s.length();
115-
Set<Long> seen = new HashSet<>();
114+
Set<Long> vis = new HashSet<>();
116115
for (int i = 1; i + len - 1 <= n; ++i) {
117116
int j = i + len - 1;
118117
long t = h[j] - h[i - 1] * p[j - i + 1];
119-
if (seen.contains(t)) {
118+
if (vis.contains(t)) {
120119
return s.substring(i - 1, j);
121120
}
122-
seen.add(t);
121+
vis.add(t);
123122
}
124123
return "";
125124
}
@@ -159,15 +158,15 @@ public:
159158
return ans;
160159
}
161160

162-
string check(string s, int len) {
161+
string check(string& s, int len) {
163162
int n = s.size();
164-
unordered_set<ULL> seen;
163+
unordered_set<ULL> vis;
165164
for (int i = 1; i + len - 1 <= n; ++i)
166165
{
167166
int j = i + len - 1;
168167
ULL t = h[j] - h[i - 1] * p[j - i + 1];
169-
if (seen.count(t)) return s.substr(i - 1, len);
170-
seen.insert(t);
168+
if (vis.count(t)) return s.substr(i - 1, len);
169+
vis.insert(t);
171170
}
172171
return "";
173172
}
@@ -187,14 +186,14 @@ func longestDupSubstring(s string) string {
187186
h[i+1] = h[i]*int64(base) + int64(s[i])
188187
}
189188
check := func(l int) string {
190-
seen := make(map[int64]bool)
189+
vis := make(map[int64]bool)
191190
for i := 1; i+l-1 <= n; i++ {
192191
j := i + l - 1
193192
t := h[j] - h[i-1]*p[j-i+1]
194-
if seen[t] {
193+
if vis[t] {
195194
return s[i-1 : j]
196195
}
197-
seen[t] = true
196+
vis[t] = true
198197
}
199198
return ""
200199
}

solution/1000-1099/1044.Longest Duplicate Substring/README_EN.md

+15-16
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,23 @@
3333
```python
3434
class Solution:
3535
def longestDupSubstring(self, s: str) -> str:
36-
n = len(s)
37-
3836
def check(l):
39-
seen = set()
37+
vis = set()
4038
for i in range(n - l + 1):
4139
t = s[i: i + l]
42-
if t in seen:
40+
if t in vis:
4341
return t
44-
seen.add(t)
42+
vis.add(t)
4543
return ''
4644

45+
n = len(s)
4746
left, right = 0, n
4847
ans = ''
4948
while left < right:
5049
mid = (left + right + 1) >> 1
5150
t = check(mid)
5251
ans = t or ans
53-
if len(t) > 0:
52+
if t:
5453
left = mid
5554
else:
5655
right = mid - 1
@@ -91,14 +90,14 @@ class Solution {
9190

9291
private String check(String s, int len) {
9392
int n = s.length();
94-
Set<Long> seen = new HashSet<>();
93+
Set<Long> vis = new HashSet<>();
9594
for (int i = 1; i + len - 1 <= n; ++i) {
9695
int j = i + len - 1;
9796
long t = h[j] - h[i - 1] * p[j - i + 1];
98-
if (seen.contains(t)) {
97+
if (vis.contains(t)) {
9998
return s.substring(i - 1, j);
10099
}
101-
seen.add(t);
100+
vis.add(t);
102101
}
103102
return "";
104103
}
@@ -138,15 +137,15 @@ public:
138137
return ans;
139138
}
140139

141-
string check(string s, int len) {
140+
string check(string& s, int len) {
142141
int n = s.size();
143-
unordered_set<ULL> seen;
142+
unordered_set<ULL> vis;
144143
for (int i = 1; i + len - 1 <= n; ++i)
145144
{
146145
int j = i + len - 1;
147146
ULL t = h[j] - h[i - 1] * p[j - i + 1];
148-
if (seen.count(t)) return s.substr(i - 1, len);
149-
seen.insert(t);
147+
if (vis.count(t)) return s.substr(i - 1, len);
148+
vis.insert(t);
150149
}
151150
return "";
152151
}
@@ -166,14 +165,14 @@ func longestDupSubstring(s string) string {
166165
h[i+1] = h[i]*int64(base) + int64(s[i])
167166
}
168167
check := func(l int) string {
169-
seen := make(map[int64]bool)
168+
vis := make(map[int64]bool)
170169
for i := 1; i+l-1 <= n; i++ {
171170
j := i + l - 1
172171
t := h[j] - h[i-1]*p[j-i+1]
173-
if seen[t] {
172+
if vis[t] {
174173
return s[i-1 : j]
175174
}
176-
seen[t] = true
175+
vis[t] = true
177176
}
178177
return ""
179178
}

solution/1000-1099/1044.Longest Duplicate Substring/Solution.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ class Solution {
2828
return ans;
2929
}
3030

31-
string check(string s, int len) {
31+
string check(string& s, int len) {
3232
int n = s.size();
33-
unordered_set<ULL> seen;
33+
unordered_set<ULL> vis;
3434
for (int i = 1; i + len - 1 <= n; ++i)
3535
{
3636
int j = i + len - 1;
3737
ULL t = h[j] - h[i - 1] * p[j - i + 1];
38-
if (seen.count(t)) return s.substr(i - 1, len);
39-
seen.insert(t);
38+
if (vis.count(t)) return s.substr(i - 1, len);
39+
vis.insert(t);
4040
}
4141
return "";
4242
}

solution/1000-1099/1044.Longest Duplicate Substring/Solution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ func longestDupSubstring(s string) string {
88
h[i+1] = h[i]*int64(base) + int64(s[i])
99
}
1010
check := func(l int) string {
11-
seen := make(map[int64]bool)
11+
vis := make(map[int64]bool)
1212
for i := 1; i+l-1 <= n; i++ {
1313
j := i + l - 1
1414
t := h[j] - h[i-1]*p[j-i+1]
15-
if seen[t] {
15+
if vis[t] {
1616
return s[i-1 : j]
1717
}
18-
seen[t] = true
18+
vis[t] = true
1919
}
2020
return ""
2121
}

solution/1000-1099/1044.Longest Duplicate Substring/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public String longestDupSubstring(String s) {
2929

3030
private String check(String s, int len) {
3131
int n = s.length();
32-
Set<Long> seen = new HashSet<>();
32+
Set<Long> vis = new HashSet<>();
3333
for (int i = 1; i + len - 1 <= n; ++i) {
3434
int j = i + len - 1;
3535
long t = h[j] - h[i - 1] * p[j - i + 1];
36-
if (seen.contains(t)) {
36+
if (vis.contains(t)) {
3737
return s.substring(i - 1, j);
3838
}
39-
seen.add(t);
39+
vis.add(t);
4040
}
4141
return "";
4242
}

solution/1000-1099/1044.Longest Duplicate Substring/Solution.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
class Solution:
22
def longestDupSubstring(self, s: str) -> str:
3-
n = len(s)
4-
53
def check(l):
6-
seen = set()
4+
vis = set()
75
for i in range(n - l + 1):
8-
t = s[i : i + l]
9-
if t in seen:
6+
t = s[i: i + l]
7+
if t in vis:
108
return t
11-
seen.add(t)
9+
vis.add(t)
1210
return ''
1311

12+
n = len(s)
1413
left, right = 0, n
1514
ans = ''
1615
while left < right:
1716
mid = (left + right + 1) >> 1
1817
t = check(mid)
1918
ans = t or ans
20-
if len(t) > 0:
19+
if t:
2120
left = mid
2221
else:
2322
right = mid - 1

0 commit comments

Comments
 (0)