feat: update solutions to lc problem: No.1044

No.1044.Longest Duplicate Substring
This commit is contained in:
yanglbme 2022-05-20 03:14:25 +00:00 committed by GitHub
parent e419d0a18b
commit 466936070a
8 changed files with 50 additions and 51 deletions

View File

@ -63,6 +63,7 @@
- [单词规律 II](/solution/0200-0299/0291.Word%20Pattern%20II/README.md) - `哈希表`、`回溯`
- [最短回文串](/solution/0200-0299/0214.Shortest%20Palindrome/README.md) - `字符串哈希`
- [回文对](/solution/0300-0399/0336.Palindrome%20Pairs/README.md) - `字符串哈希`
- [最长重复子串](/solution/1000-1099/1044.Longest%20Duplicate%20Substring/README.md) - `字符串哈希` - `二分查找`
### 3. 搜索

View File

@ -60,7 +60,8 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
- [Constrained Subsequence Sum](/solution/1400-1499/1425.Constrained%20Subsequence%20Sum/README_EN.md) - `Dynamic Programming`, `Monotonic Queue`
- [Word Pattern II](/solution/0200-0299/0291.Word%20Pattern%20II/README_EN.md) - `Hash Table`、`Backtracking`
- [Shortest Palindrome](/solution/0200-0299/0214.Shortest%20Palindrome/README_EN.md) - `Rabin-Karp`
- [Palindrome Pairs](/solution/0300-0399/0336.Palindrome%20Pairs/README_EN.md)) - `Rabin-Karp`
- [Palindrome Pairs](/solution/0300-0399/0336.Palindrome%20Pairs/README_EN.md) - `Rabin-Karp`
- [Longest Duplicate Substring](/solution/1000-1099/1044.Longest%20Duplicate%20Substring/README_EN.md) - `Rabin-Karp`, `Binary search`
### 3. Search

View File

@ -39,7 +39,7 @@
<!-- 这里可写通用的实现逻辑 -->
字符串哈希 + 二分查找。
**方法一:字符串哈希 + 二分查找**
字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。二分枚举长度,找到满足条件的最大长度即可。
@ -52,24 +52,23 @@
```python
class Solution:
def longestDupSubstring(self, s: str) -> str:
n = len(s)
def check(l):
seen = set()
vis = set()
for i in range(n - l + 1):
t = s[i: i + l]
if t in seen:
if t in vis:
return t
seen.add(t)
vis.add(t)
return ''
n = len(s)
left, right = 0, n
ans = ''
while left < right:
mid = (left + right + 1) >> 1
t = check(mid)
ans = t or ans
if len(t) > 0:
if t:
left = mid
else:
right = mid - 1
@ -112,14 +111,14 @@ class Solution {
private String check(String s, int len) {
int n = s.length();
Set<Long> seen = new HashSet<>();
Set<Long> vis = new HashSet<>();
for (int i = 1; i + len - 1 <= n; ++i) {
int j = i + len - 1;
long t = h[j] - h[i - 1] * p[j - i + 1];
if (seen.contains(t)) {
if (vis.contains(t)) {
return s.substring(i - 1, j);
}
seen.add(t);
vis.add(t);
}
return "";
}
@ -159,15 +158,15 @@ public:
return ans;
}
string check(string s, int len) {
string check(string& s, int len) {
int n = s.size();
unordered_set<ULL> seen;
unordered_set<ULL> vis;
for (int i = 1; i + len - 1 <= n; ++i)
{
int j = i + len - 1;
ULL t = h[j] - h[i - 1] * p[j - i + 1];
if (seen.count(t)) return s.substr(i - 1, len);
seen.insert(t);
if (vis.count(t)) return s.substr(i - 1, len);
vis.insert(t);
}
return "";
}
@ -187,14 +186,14 @@ func longestDupSubstring(s string) string {
h[i+1] = h[i]*int64(base) + int64(s[i])
}
check := func(l int) string {
seen := make(map[int64]bool)
vis := make(map[int64]bool)
for i := 1; i+l-1 <= n; i++ {
j := i + l - 1
t := h[j] - h[i-1]*p[j-i+1]
if seen[t] {
if vis[t] {
return s[i-1 : j]
}
seen[t] = true
vis[t] = true
}
return ""
}

View File

@ -33,24 +33,23 @@
```python
class Solution:
def longestDupSubstring(self, s: str) -> str:
n = len(s)
def check(l):
seen = set()
vis = set()
for i in range(n - l + 1):
t = s[i: i + l]
if t in seen:
if t in vis:
return t
seen.add(t)
vis.add(t)
return ''
n = len(s)
left, right = 0, n
ans = ''
while left < right:
mid = (left + right + 1) >> 1
t = check(mid)
ans = t or ans
if len(t) > 0:
if t:
left = mid
else:
right = mid - 1
@ -91,14 +90,14 @@ class Solution {
private String check(String s, int len) {
int n = s.length();
Set<Long> seen = new HashSet<>();
Set<Long> vis = new HashSet<>();
for (int i = 1; i + len - 1 <= n; ++i) {
int j = i + len - 1;
long t = h[j] - h[i - 1] * p[j - i + 1];
if (seen.contains(t)) {
if (vis.contains(t)) {
return s.substring(i - 1, j);
}
seen.add(t);
vis.add(t);
}
return "";
}
@ -138,15 +137,15 @@ public:
return ans;
}
string check(string s, int len) {
string check(string& s, int len) {
int n = s.size();
unordered_set<ULL> seen;
unordered_set<ULL> vis;
for (int i = 1; i + len - 1 <= n; ++i)
{
int j = i + len - 1;
ULL t = h[j] - h[i - 1] * p[j - i + 1];
if (seen.count(t)) return s.substr(i - 1, len);
seen.insert(t);
if (vis.count(t)) return s.substr(i - 1, len);
vis.insert(t);
}
return "";
}
@ -166,14 +165,14 @@ func longestDupSubstring(s string) string {
h[i+1] = h[i]*int64(base) + int64(s[i])
}
check := func(l int) string {
seen := make(map[int64]bool)
vis := make(map[int64]bool)
for i := 1; i+l-1 <= n; i++ {
j := i + l - 1
t := h[j] - h[i-1]*p[j-i+1]
if seen[t] {
if vis[t] {
return s[i-1 : j]
}
seen[t] = true
vis[t] = true
}
return ""
}

View File

@ -28,15 +28,15 @@ public:
return ans;
}
string check(string s, int len) {
string check(string& s, int len) {
int n = s.size();
unordered_set<ULL> seen;
unordered_set<ULL> vis;
for (int i = 1; i + len - 1 <= n; ++i)
{
int j = i + len - 1;
ULL t = h[j] - h[i - 1] * p[j - i + 1];
if (seen.count(t)) return s.substr(i - 1, len);
seen.insert(t);
if (vis.count(t)) return s.substr(i - 1, len);
vis.insert(t);
}
return "";
}

View File

@ -8,14 +8,14 @@ func longestDupSubstring(s string) string {
h[i+1] = h[i]*int64(base) + int64(s[i])
}
check := func(l int) string {
seen := make(map[int64]bool)
vis := make(map[int64]bool)
for i := 1; i+l-1 <= n; i++ {
j := i + l - 1
t := h[j] - h[i-1]*p[j-i+1]
if seen[t] {
if vis[t] {
return s[i-1 : j]
}
seen[t] = true
vis[t] = true
}
return ""
}

View File

@ -29,14 +29,14 @@ class Solution {
private String check(String s, int len) {
int n = s.length();
Set<Long> seen = new HashSet<>();
Set<Long> vis = new HashSet<>();
for (int i = 1; i + len - 1 <= n; ++i) {
int j = i + len - 1;
long t = h[j] - h[i - 1] * p[j - i + 1];
if (seen.contains(t)) {
if (vis.contains(t)) {
return s.substring(i - 1, j);
}
seen.add(t);
vis.add(t);
}
return "";
}

View File

@ -1,23 +1,22 @@
class Solution:
def longestDupSubstring(self, s: str) -> str:
n = len(s)
def check(l):
seen = set()
vis = set()
for i in range(n - l + 1):
t = s[i : i + l]
if t in seen:
t = s[i: i + l]
if t in vis:
return t
seen.add(t)
vis.add(t)
return ''
n = len(s)
left, right = 0, n
ans = ''
while left < right:
mid = (left + right + 1) >> 1
t = check(mid)
ans = t or ans
if len(t) > 0:
if t:
left = mid
else:
right = mid - 1