Skip to content

Commit 7eb6955

Browse files
authored
feat: add solutions to lc problem: No.1316
No.1316.Distinct Echo Substrings
1 parent 4669360 commit 7eb6955

File tree

8 files changed

+371
-3
lines changed

8 files changed

+371
-3
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
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) - `字符串哈希` - `二分查找`
66+
- [最长重复子串](/solution/1000-1099/1044.Longest%20Duplicate%20Substring/README.md) - `字符串哈希``二分查找`
67+
- [不同的循环子字符串](/solution/1300-1399/1316.Distinct%20Echo%20Substrings/README.md) - `字符串哈希`
6768

6869
### 3. 搜索
6970

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
6262
- [Shortest Palindrome](/solution/0200-0299/0214.Shortest%20Palindrome/README_EN.md) - `Rabin-Karp`
6363
- [Palindrome Pairs](/solution/0300-0399/0336.Palindrome%20Pairs/README_EN.md) - `Rabin-Karp`
6464
- [Longest Duplicate Substring](/solution/1000-1099/1044.Longest%20Duplicate%20Substring/README_EN.md) - `Rabin-Karp`, `Binary search`
65+
- [Distinct Echo Substrings](/solution/1300-1399/1316.Distinct%20Echo%20Substrings/README_EN.md) - `Rabin-Karp`
6566

6667
### 3. Search
6768

solution/1300-1399/1316.Distinct Echo Substrings/README.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,148 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:字符串哈希**
47+
4648
<!-- tabs:start -->
4749

4850
### **Python3**
4951

5052
<!-- 这里可写当前语言的特殊实现逻辑 -->
5153

5254
```python
53-
55+
class Solution:
56+
def distinctEchoSubstrings(self, text: str) -> int:
57+
def get(l, r):
58+
return (h[r] - h[l - 1] * p[r - l + 1]) % mod
59+
60+
n = len(text)
61+
base = 131
62+
mod = int(1e9) + 7
63+
h = [0] * (n + 10)
64+
p = [1] * (n + 10)
65+
for i, c in enumerate(text):
66+
t = ord(c) - ord('a') + 1
67+
h[i + 1] = (h[i] * base) % mod + t
68+
p[i + 1] = (p[i] * base) % mod
69+
vis = set()
70+
for i in range(n - 1):
71+
for j in range(i + 1, n, 2):
72+
k = (i + j) >> 1
73+
a = get(i + 1, k + 1)
74+
b = get(k + 2, j + 1)
75+
if a == b:
76+
vis.add(a)
77+
return len(vis)
5478
```
5579

5680
### **Java**
5781

5882
<!-- 这里可写当前语言的特殊实现逻辑 -->
5983

6084
```java
85+
class Solution {
86+
private long[] h;
87+
private long[] p;
88+
89+
public int distinctEchoSubstrings(String text) {
90+
int n = text.length();
91+
int base = 131;
92+
h = new long[n + 10];
93+
p = new long[n + 10];
94+
p[0] = 1;
95+
for (int i = 0; i < n; ++i) {
96+
int t = text.charAt(i) - 'a' + 1;
97+
h[i + 1] = h[i] * base + t;
98+
p[i + 1] = p[i] * base;
99+
}
100+
Set<Long> vis = new HashSet<>();
101+
for (int i = 0; i < n - 1; ++i) {
102+
for (int j = i + 1; j < n; j += 2) {
103+
int k = (i + j) >> 1;
104+
long a = get(i + 1, k + 1);
105+
long b = get(k + 2, j + 1);
106+
if (a == b) {
107+
vis.add(a);
108+
}
109+
}
110+
}
111+
return vis.size();
112+
}
113+
114+
private long get(int i, int j) {
115+
return h[j] - h[i - 1] * p[j - i + 1];
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
typedef unsigned long long ull;
124+
125+
class Solution {
126+
public:
127+
int distinctEchoSubstrings(string text) {
128+
int n = text.size();
129+
int base = 131;
130+
vector<ull> p(n + 10);
131+
vector<ull> h(n + 10);
132+
p[0] = 1;
133+
for (int i = 0; i < n; ++i)
134+
{
135+
int t = text[i] - 'a' + 1;
136+
p[i + 1] = p[i] * base;
137+
h[i + 1] = h[i] * base + t;
138+
}
139+
unordered_set<ull> vis;
140+
for (int i = 0; i < n - 1; ++i)
141+
{
142+
for (int j = i + 1; j < n; j += 2)
143+
{
144+
int k = (i + j) >> 1;
145+
ull a = get(i + 1, k + 1, p, h);
146+
ull b = get(k + 2, j + 1, p, h);
147+
if (a == b) vis.insert(a);
148+
}
149+
}
150+
return vis.size();
151+
}
152+
153+
ull get(int l, int r, vector<ull>& p, vector<ull>& h) {
154+
return h[r] - h[l - 1] * p[r - l + 1];
155+
}
156+
};
157+
```
61158

159+
### **Go**
160+
161+
```go
162+
func distinctEchoSubstrings(text string) int {
163+
n := len(text)
164+
base := 131
165+
h := make([]int, n+10)
166+
p := make([]int, n+10)
167+
p[0] = 1
168+
for i, c := range text {
169+
t := int(c-'a') + 1
170+
p[i+1] = p[i] * base
171+
h[i+1] = h[i]*base + t
172+
}
173+
get := func(l, r int) int {
174+
return h[r] - h[l-1]*p[r-l+1]
175+
}
176+
vis := map[int]bool{}
177+
for i := 0; i < n-1; i++ {
178+
for j := i + 1; j < n; j += 2 {
179+
k := (i + j) >> 1
180+
a, b := get(i+1, k+1), get(k+2, j+1)
181+
if a == b {
182+
vis[a] = true
183+
}
184+
}
185+
}
186+
return len(vis)
187+
}
62188
```
63189

64190
### **...**

solution/1300-1399/1316.Distinct Echo Substrings/README_EN.md

+125-1
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,137 @@
3838
### **Python3**
3939

4040
```python
41-
41+
class Solution:
42+
def distinctEchoSubstrings(self, text: str) -> int:
43+
def get(l, r):
44+
return (h[r] - h[l - 1] * p[r - l + 1]) % mod
45+
46+
n = len(text)
47+
base = 131
48+
mod = int(1e9) + 7
49+
h = [0] * (n + 10)
50+
p = [1] * (n + 10)
51+
for i, c in enumerate(text):
52+
t = ord(c) - ord('a') + 1
53+
h[i + 1] = (h[i] * base) % mod + t
54+
p[i + 1] = (p[i] * base) % mod
55+
vis = set()
56+
for i in range(n - 1):
57+
for j in range(i + 1, n, 2):
58+
k = (i + j) >> 1
59+
a = get(i + 1, k + 1)
60+
b = get(k + 2, j + 1)
61+
if a == b:
62+
vis.add(a)
63+
return len(vis)
4264
```
4365

4466
### **Java**
4567

4668
```java
69+
class Solution {
70+
private long[] h;
71+
private long[] p;
72+
73+
public int distinctEchoSubstrings(String text) {
74+
int n = text.length();
75+
int base = 131;
76+
h = new long[n + 10];
77+
p = new long[n + 10];
78+
p[0] = 1;
79+
for (int i = 0; i < n; ++i) {
80+
int t = text.charAt(i) - 'a' + 1;
81+
h[i + 1] = h[i] * base + t;
82+
p[i + 1] = p[i] * base;
83+
}
84+
Set<Long> vis = new HashSet<>();
85+
for (int i = 0; i < n - 1; ++i) {
86+
for (int j = i + 1; j < n; j += 2) {
87+
int k = (i + j) >> 1;
88+
long a = get(i + 1, k + 1);
89+
long b = get(k + 2, j + 1);
90+
if (a == b) {
91+
vis.add(a);
92+
}
93+
}
94+
}
95+
return vis.size();
96+
}
97+
98+
private long get(int i, int j) {
99+
return h[j] - h[i - 1] * p[j - i + 1];
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
typedef unsigned long long ull;
108+
109+
class Solution {
110+
public:
111+
int distinctEchoSubstrings(string text) {
112+
int n = text.size();
113+
int base = 131;
114+
vector<ull> p(n + 10);
115+
vector<ull> h(n + 10);
116+
p[0] = 1;
117+
for (int i = 0; i < n; ++i)
118+
{
119+
int t = text[i] - 'a' + 1;
120+
p[i + 1] = p[i] * base;
121+
h[i + 1] = h[i] * base + t;
122+
}
123+
unordered_set<ull> vis;
124+
for (int i = 0; i < n - 1; ++i)
125+
{
126+
for (int j = i + 1; j < n; j += 2)
127+
{
128+
int k = (i + j) >> 1;
129+
ull a = get(i + 1, k + 1, p, h);
130+
ull b = get(k + 2, j + 1, p, h);
131+
if (a == b) vis.insert(a);
132+
}
133+
}
134+
return vis.size();
135+
}
136+
137+
ull get(int l, int r, vector<ull>& p, vector<ull>& h) {
138+
return h[r] - h[l - 1] * p[r - l + 1];
139+
}
140+
};
141+
```
47142

143+
### **Go**
144+
145+
```go
146+
func distinctEchoSubstrings(text string) int {
147+
n := len(text)
148+
base := 131
149+
h := make([]int, n+10)
150+
p := make([]int, n+10)
151+
p[0] = 1
152+
for i, c := range text {
153+
t := int(c-'a') + 1
154+
p[i+1] = p[i] * base
155+
h[i+1] = h[i]*base + t
156+
}
157+
get := func(l, r int) int {
158+
return h[r] - h[l-1]*p[r-l+1]
159+
}
160+
vis := map[int]bool{}
161+
for i := 0; i < n-1; i++ {
162+
for j := i + 1; j < n; j += 2 {
163+
k := (i + j) >> 1
164+
a, b := get(i+1, k+1), get(k+2, j+1)
165+
if a == b {
166+
vis[a] = true
167+
}
168+
}
169+
}
170+
return len(vis)
171+
}
48172
```
49173

50174
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
typedef unsigned long long ull;
2+
3+
class Solution {
4+
public:
5+
int distinctEchoSubstrings(string text) {
6+
int n = text.size();
7+
int base = 131;
8+
vector<ull> p(n + 10);
9+
vector<ull> h(n + 10);
10+
p[0] = 1;
11+
for (int i = 0; i < n; ++i)
12+
{
13+
int t = text[i] - 'a' + 1;
14+
p[i + 1] = p[i] * base;
15+
h[i + 1] = h[i] * base + t;
16+
}
17+
unordered_set<ull> vis;
18+
for (int i = 0; i < n - 1; ++i)
19+
{
20+
for (int j = i + 1; j < n; j += 2)
21+
{
22+
int k = (i + j) >> 1;
23+
ull a = get(i + 1, k + 1, p, h);
24+
ull b = get(k + 2, j + 1, p, h);
25+
if (a == b) vis.insert(a);
26+
}
27+
}
28+
return vis.size();
29+
}
30+
31+
ull get(int l, int r, vector<ull>& p, vector<ull>& h) {
32+
return h[r] - h[l - 1] * p[r - l + 1];
33+
}
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func distinctEchoSubstrings(text string) int {
2+
n := len(text)
3+
base := 131
4+
h := make([]int, n+10)
5+
p := make([]int, n+10)
6+
p[0] = 1
7+
for i, c := range text {
8+
t := int(c-'a') + 1
9+
p[i+1] = p[i] * base
10+
h[i+1] = h[i]*base + t
11+
}
12+
get := func(l, r int) int {
13+
return h[r] - h[l-1]*p[r-l+1]
14+
}
15+
vis := map[int]bool{}
16+
for i := 0; i < n-1; i++ {
17+
for j := i + 1; j < n; j += 2 {
18+
k := (i + j) >> 1
19+
a, b := get(i+1, k+1), get(k+2, j+1)
20+
if a == b {
21+
vis[a] = true
22+
}
23+
}
24+
}
25+
return len(vis)
26+
}

0 commit comments

Comments
 (0)