Skip to content

Commit 1240ef1

Browse files
committed
feat: add solutions to lc problem: No.1062
No.1062.Longest Repeating Substring
1 parent 6fe9df5 commit 1240ef1

File tree

7 files changed

+253
-4
lines changed

7 files changed

+253
-4
lines changed

solution/0800-0899/0801.Minimum Swaps To Make Sequences Increasing/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ A = [1, 3, 5, 7] , B = [1, 2, 3, 4]
5757

5858
**方法一:动态规划**
5959

60-
定义 $a$, $b$ 分别表示使得前 $i$ 个元素序列严格递增,且第 $i$ 个元素不交换、交换的最小交换次数。
60+
定义 $a$, $b$ 分别表示使得下标 $[0..i]$ 的元素序列严格递增,且第 $i$ 个元素不交换、交换的最小交换次数。下标从 $0$ 开始
6161

62-
当 $i=0$ 时,易知 $a = 0$, $b=1$。
62+
当 $i=0$ 时, $a = 0$, $b=1$。
6363

6464
当 $i\gt 0$ 时,我们先将此前 $a$, $b$ 的值保存在 $x$, $y$ 中,然后分情况讨论:
6565

solution/1000-1099/1062.Longest Repeating Substring/README.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,119 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:动态规划**
54+
55+
定义 $dp[i][j]$ 表示以 $s[i]$ 和 $s[j]$ 结尾的最长重复子串的长度。状态转移方程为:
56+
57+
$$
58+
dp[i][j]=
59+
\begin{cases}
60+
dp[i-1][j-1]+1, & i>0 \cap s[i]=s[j] \\
61+
1, & i=0 \cap s[i]=s[j] \\
62+
0, & s[i] \neq s[j]
63+
\end{cases}
64+
$$
65+
66+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。
67+
68+
其中 $n$ 为字符串 $s$ 的长度。
69+
5370
<!-- tabs:start -->
5471

5572
### **Python3**
5673

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

5976
```python
60-
77+
class Solution:
78+
def longestRepeatingSubstring(self, s: str) -> int:
79+
n = len(s)
80+
dp = [[0] * n for _ in range(n)]
81+
ans = 0
82+
for i in range(n):
83+
for j in range(i + 1, n):
84+
if s[i] == s[j]:
85+
dp[i][j] = dp[i - 1][j - 1] + 1 if i else 1
86+
ans = max(ans, dp[i][j])
87+
return ans
6188
```
6289

6390
### **Java**
6491

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

6794
```java
95+
class Solution {
96+
public int longestRepeatingSubstring(String s) {
97+
int n = s.length();
98+
int ans = 0;
99+
int[][] dp = new int[n][n];
100+
for (int i = 0; i < n; ++i) {
101+
for (int j = i + 1; j < n; ++j) {
102+
if (s.charAt(i) == s.charAt(j)) {
103+
dp[i][j] = i > 0 ? dp[i - 1][j - 1] + 1 : 1;
104+
ans = Math.max(ans, dp[i][j]);
105+
}
106+
}
107+
}
108+
return ans;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
int longestRepeatingSubstring(string s) {
119+
int n = s.size();
120+
vector<vector<int>> dp(n, vector<int>(n));
121+
int ans = 0;
122+
for (int i = 0; i < n; ++i) {
123+
for (int j = i + 1; j < n; ++j) {
124+
if (s[i] == s[j]) {
125+
dp[i][j] = i ? dp[i - 1][j - 1] + 1 : 1;
126+
ans = max(ans, dp[i][j]);
127+
}
128+
}
129+
}
130+
return ans;
131+
}
132+
};
133+
```
68134
135+
### **Go**
136+
137+
```go
138+
func longestRepeatingSubstring(s string) int {
139+
n := len(s)
140+
dp := make([][]int, n)
141+
for i := range dp {
142+
dp[i] = make([]int, n)
143+
}
144+
ans := 0
145+
for i := 0; i < n; i++ {
146+
for j := i + 1; j < n; j++ {
147+
if s[i] == s[j] {
148+
if i == 0 {
149+
dp[i][j] = 1
150+
} else {
151+
dp[i][j] = dp[i-1][j-1] + 1
152+
}
153+
ans = max(ans, dp[i][j])
154+
}
155+
}
156+
}
157+
return ans
158+
}
159+
160+
func max(a, b int) int {
161+
if a > b {
162+
return a
163+
}
164+
return b
165+
}
69166
```
70167

71168
### **...**

solution/1000-1099/1062.Longest Repeating Substring/README_EN.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,93 @@
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def longestRepeatingSubstring(self, s: str) -> int:
51+
n = len(s)
52+
dp = [[0] * n for _ in range(n)]
53+
ans = 0
54+
for i in range(n):
55+
for j in range(i + 1, n):
56+
if s[i] == s[j]:
57+
dp[i][j] = dp[i - 1][j - 1] + 1 if i else 1
58+
ans = max(ans, dp[i][j])
59+
return ans
5060
```
5161

5262
### **Java**
5363

5464
```java
65+
class Solution {
66+
public int longestRepeatingSubstring(String s) {
67+
int n = s.length();
68+
int ans = 0;
69+
int[][] dp = new int[n][n];
70+
for (int i = 0; i < n; ++i) {
71+
for (int j = i + 1; j < n; ++j) {
72+
if (s.charAt(i) == s.charAt(j)) {
73+
dp[i][j] = i > 0 ? dp[i - 1][j - 1] + 1 : 1;
74+
ans = Math.max(ans, dp[i][j]);
75+
}
76+
}
77+
}
78+
return ans;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int longestRepeatingSubstring(string s) {
89+
int n = s.size();
90+
vector<vector<int>> dp(n, vector<int>(n));
91+
int ans = 0;
92+
for (int i = 0; i < n; ++i) {
93+
for (int j = i + 1; j < n; ++j) {
94+
if (s[i] == s[j]) {
95+
dp[i][j] = i ? dp[i - 1][j - 1] + 1 : 1;
96+
ans = max(ans, dp[i][j]);
97+
}
98+
}
99+
}
100+
return ans;
101+
}
102+
};
103+
```
55104
105+
### **Go**
106+
107+
```go
108+
func longestRepeatingSubstring(s string) int {
109+
n := len(s)
110+
dp := make([][]int, n)
111+
for i := range dp {
112+
dp[i] = make([]int, n)
113+
}
114+
ans := 0
115+
for i := 0; i < n; i++ {
116+
for j := i + 1; j < n; j++ {
117+
if s[i] == s[j] {
118+
if i == 0 {
119+
dp[i][j] = 1
120+
} else {
121+
dp[i][j] = dp[i-1][j-1] + 1
122+
}
123+
ans = max(ans, dp[i][j])
124+
}
125+
}
126+
}
127+
return ans
128+
}
129+
130+
func max(a, b int) int {
131+
if a > b {
132+
return a
133+
}
134+
return b
135+
}
56136
```
57137

58138
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int longestRepeatingSubstring(string s) {
4+
int n = s.size();
5+
vector<vector<int>> dp(n, vector<int>(n));
6+
int ans = 0;
7+
for (int i = 0; i < n; ++i) {
8+
for (int j = i + 1; j < n; ++j) {
9+
if (s[i] == s[j]) {
10+
dp[i][j] = i ? dp[i - 1][j - 1] + 1 : 1;
11+
ans = max(ans, dp[i][j]);
12+
}
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func longestRepeatingSubstring(s string) int {
2+
n := len(s)
3+
dp := make([][]int, n)
4+
for i := range dp {
5+
dp[i] = make([]int, n)
6+
}
7+
ans := 0
8+
for i := 0; i < n; i++ {
9+
for j := i + 1; j < n; j++ {
10+
if s[i] == s[j] {
11+
if i == 0 {
12+
dp[i][j] = 1
13+
} else {
14+
dp[i][j] = dp[i-1][j-1] + 1
15+
}
16+
ans = max(ans, dp[i][j])
17+
}
18+
}
19+
}
20+
return ans
21+
}
22+
23+
func max(a, b int) int {
24+
if a > b {
25+
return a
26+
}
27+
return b
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int longestRepeatingSubstring(String s) {
3+
int n = s.length();
4+
int ans = 0;
5+
int[][] dp = new int[n][n];
6+
for (int i = 0; i < n; ++i) {
7+
for (int j = i + 1; j < n; ++j) {
8+
if (s.charAt(i) == s.charAt(j)) {
9+
dp[i][j] = i > 0 ? dp[i - 1][j - 1] + 1 : 1;
10+
ans = Math.max(ans, dp[i][j]);
11+
}
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def longestRepeatingSubstring(self, s: str) -> int:
3+
n = len(s)
4+
dp = [[0] * n for _ in range(n)]
5+
ans = 0
6+
for i in range(n):
7+
for j in range(i + 1, n):
8+
if s[i] == s[j]:
9+
dp[i][j] = dp[i - 1][j - 1] + 1 if i else 1
10+
ans = max(ans, dp[i][j])
11+
return ans

0 commit comments

Comments
 (0)