Skip to content

feat: add solutions to lc problem: No.1062 #4158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 78 additions & 36 deletions solution/1000-1099/1062.Longest Repeating Substring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,21 @@ tags:

### 方法一:动态规划

定义 $dp[i][j]$ 表示以 $s[i]$ 和 $s[j]$ 结尾的最长重复子串 🔒 的长度。状态转移方程为:
我们定义 $f[i][j]$ 表示以 $s[i]$ 和 $s[j]$ 结尾的最长重复子串的长度,初始时 $f[i][j]=0$。

我们在 $[1, n)$ 的区间内枚举 $i$,在 $[0, i)$ 的区间内枚举 $j$,如果 $s[i]=s[j]$,那么有:

$$
dp[i][j]=
f[i][j]=
\begin{cases}
dp[i-1][j-1]+1, & i>0 \cap s[i]=s[j] \\
1, & i=0 \cap s[i]=s[j] \\
0, & s[i] \neq s[j]
f[i-1][j-1]+1, & j>0 \\
1, & j=0
\end{cases}
$$

时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$
我们求出所有 $f[i][j]$ 的最大值即为答案

其中 $n$ 为字符串 $s$ 的长度。
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为字符串 $s$ 的长度。

相似题目:

Expand All @@ -93,13 +94,13 @@ $$
class Solution:
def longestRepeatingSubstring(self, s: str) -> int:
n = len(s)
dp = [[0] * n for _ in range(n)]
f = [[0] * n for _ in range(n)]
ans = 0
for i in range(n):
for j in range(i + 1, n):
for i in range(1, n):
for j in range(i):
if s[i] == s[j]:
dp[i][j] = dp[i - 1][j - 1] + 1 if i else 1
ans = max(ans, dp[i][j])
f[i][j] = 1 + (f[i - 1][j - 1] if j else 0)
ans = max(ans, f[i][j])
return ans
```

Expand All @@ -109,13 +110,13 @@ class Solution:
class Solution {
public int longestRepeatingSubstring(String s) {
int n = s.length();
int[][] f = new int[n][n];
int ans = 0;
int[][] dp = new int[n][n];
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = i > 0 ? dp[i - 1][j - 1] + 1 : 1;
ans = Math.max(ans, dp[i][j]);
f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0);
ans = Math.max(ans, f[i][j]);
}
}
}
Expand All @@ -130,14 +131,15 @@ class Solution {
class Solution {
public:
int longestRepeatingSubstring(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n));
int n = s.length();
int f[n][n];
memset(f, 0, sizeof(f));
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (s[i] == s[j]) {
dp[i][j] = i ? dp[i - 1][j - 1] + 1 : 1;
ans = max(ans, dp[i][j]);
f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0);
ans = max(ans, f[i][j]);
}
}
}
Expand All @@ -149,26 +151,66 @@ public:
#### Go

```go
func longestRepeatingSubstring(s string) int {
func longestRepeatingSubstring(s string) (ans int) {
n := len(s)
dp := make([][]int, n)
for i := range dp {
dp[i] = make([]int, n)
f := make([][]int, n)
for i := range f {
f[i] = make([]int, n)
}
ans := 0
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if s[i] == s[j] {
if i == 0 {
dp[i][j] = 1
} else {
dp[i][j] = dp[i-1][j-1] + 1
if j > 0 {
f[i][j] = f[i-1][j-1]
}
ans = max(ans, dp[i][j])
f[i][j]++
ans = max(ans, f[i][j])
}
}
}
return ans
return
}
```

#### TypeScript

```ts
function longestRepeatingSubstring(s: string): number {
const n = s.length;
const f: number[][] = Array.from({ length: n }).map(() => Array(n).fill(0));
let ans = 0;
for (let i = 1; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (s[i] === s[j]) {
f[i][j] = 1 + (f[i - 1][j - 1] || 0);
ans = Math.max(ans, f[i][j]);
}
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn longest_repeating_substring(s: String) -> i32 {
let n = s.len();
let mut f = vec![vec![0; n]; n];
let mut ans = 0;
let s = s.as_bytes();

for i in 1..n {
for j in 0..i {
if s[i] == s[j] {
f[i][j] = if j > 0 { f[i - 1][j - 1] + 1 } else { 1 };
ans = ans.max(f[i][j]);
}
}
}
ans
}
}
```

Expand Down
121 changes: 91 additions & 30 deletions solution/1000-1099/1062.Longest Repeating Substring/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,27 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

We define $f[i][j]$ to represent the length of the longest repeating substring ending with $s[i]$ and $s[j]$. Initially, $f[i][j]=0$.

We enumerate $i$ in the range $[1, n)$ and enumerate $j$ in the range $[0, i)$. If $s[i]=s[j]$, then we have:

$$
f[i][j]=
\begin{cases}
f[i-1][j-1]+1, & j>0 \\
1, & j=0
\end{cases}
$$

The answer is the maximum value of all $f[i][j]$.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the length of the string $s$.

Similar problems:

- [1044. Longest Duplicate Substring 🔒](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1044.Longest%20Duplicate%20Substring/README_EN.md)

<!-- tabs:start -->

Expand All @@ -72,13 +92,13 @@ tags:
class Solution:
def longestRepeatingSubstring(self, s: str) -> int:
n = len(s)
dp = [[0] * n for _ in range(n)]
f = [[0] * n for _ in range(n)]
ans = 0
for i in range(n):
for j in range(i + 1, n):
for i in range(1, n):
for j in range(i):
if s[i] == s[j]:
dp[i][j] = dp[i - 1][j - 1] + 1 if i else 1
ans = max(ans, dp[i][j])
f[i][j] = 1 + (f[i - 1][j - 1] if j else 0)
ans = max(ans, f[i][j])
return ans
```

Expand All @@ -88,13 +108,13 @@ class Solution:
class Solution {
public int longestRepeatingSubstring(String s) {
int n = s.length();
int[][] f = new int[n][n];
int ans = 0;
int[][] dp = new int[n][n];
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = i > 0 ? dp[i - 1][j - 1] + 1 : 1;
ans = Math.max(ans, dp[i][j]);
f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0);
ans = Math.max(ans, f[i][j]);
}
}
}
Expand All @@ -109,14 +129,15 @@ class Solution {
class Solution {
public:
int longestRepeatingSubstring(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n));
int n = s.length();
int f[n][n];
memset(f, 0, sizeof(f));
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (s[i] == s[j]) {
dp[i][j] = i ? dp[i - 1][j - 1] + 1 : 1;
ans = max(ans, dp[i][j]);
f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0);
ans = max(ans, f[i][j]);
}
}
}
Expand All @@ -128,26 +149,66 @@ public:
#### Go

```go
func longestRepeatingSubstring(s string) int {
func longestRepeatingSubstring(s string) (ans int) {
n := len(s)
dp := make([][]int, n)
for i := range dp {
dp[i] = make([]int, n)
f := make([][]int, n)
for i := range f {
f[i] = make([]int, n)
}
ans := 0
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if s[i] == s[j] {
if i == 0 {
dp[i][j] = 1
} else {
dp[i][j] = dp[i-1][j-1] + 1
if j > 0 {
f[i][j] = f[i-1][j-1]
}
ans = max(ans, dp[i][j])
f[i][j]++
ans = max(ans, f[i][j])
}
}
}
return ans
return
}
```

#### TypeScript

```ts
function longestRepeatingSubstring(s: string): number {
const n = s.length;
const f: number[][] = Array.from({ length: n }).map(() => Array(n).fill(0));
let ans = 0;
for (let i = 1; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (s[i] === s[j]) {
f[i][j] = 1 + (f[i - 1][j - 1] || 0);
ans = Math.max(ans, f[i][j]);
}
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn longest_repeating_substring(s: String) -> i32 {
let n = s.len();
let mut f = vec![vec![0; n]; n];
let mut ans = 0;
let s = s.as_bytes();

for i in 1..n {
for j in 0..i {
if s[i] == s[j] {
f[i][j] = if j > 0 { f[i - 1][j - 1] + 1 } else { 1 };
ans = ans.max(f[i][j]);
}
}
}
ans
}
}
```

Expand Down
15 changes: 8 additions & 7 deletions solution/1000-1099/1062.Longest Repeating Substring/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
class Solution {
public:
int longestRepeatingSubstring(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n));
int n = s.length();
int f[n][n];
memset(f, 0, sizeof(f));
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (s[i] == s[j]) {
dp[i][j] = i ? dp[i - 1][j - 1] + 1 : 1;
ans = max(ans, dp[i][j]);
f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0);
ans = max(ans, f[i][j]);
}
}
}
return ans;
}
};
};
26 changes: 12 additions & 14 deletions solution/1000-1099/1062.Longest Repeating Substring/Solution.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
func longestRepeatingSubstring(s string) int {
func longestRepeatingSubstring(s string) (ans int) {
n := len(s)
dp := make([][]int, n)
for i := range dp {
dp[i] = make([]int, n)
f := make([][]int, n)
for i := range f {
f[i] = make([]int, n)
}
ans := 0
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if s[i] == s[j] {
if i == 0 {
dp[i][j] = 1
} else {
dp[i][j] = dp[i-1][j-1] + 1
if j > 0 {
f[i][j] = f[i-1][j-1]
}
ans = max(ans, dp[i][j])
f[i][j]++
ans = max(ans, f[i][j])
}
}
}
return ans
}
return
}
Loading