Skip to content

Commit ab41b45

Browse files
committed
feat: add solutions to lc problem: No.1668
No.1668.Maximum Repeating Substring
1 parent 6635b3c commit ab41b45

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

solution/1600-1699/1668.Maximum Repeating Substring/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,73 @@ func maxRepeating(sequence string, word string) int {
121121
}
122122
```
123123

124+
### **C**
125+
126+
```c
127+
#define max(a,b) (((a) > (b)) ? (a) : (b))
128+
129+
int findWord(int i, char *sequence, char *word) {
130+
int n = strlen(word);
131+
for (int j = 0; j < n; j++) {
132+
if (sequence[j + i] != word[j]) {
133+
return 0;
134+
}
135+
}
136+
return 1 + findWord(i + n, sequence, word);
137+
}
138+
139+
int maxRepeating(char *sequence, char *word) {
140+
int n = strlen(sequence);
141+
int m = strlen(word);
142+
int ans = 0;
143+
for (int i = 0; i <= n - m; i++) {
144+
ans = max(ans, findWord(i, sequence, word));
145+
}
146+
return ans;
147+
}
148+
```
149+
150+
### **TypeScript**
151+
152+
```ts
153+
function maxRepeating(sequence: string, word: string): number {
154+
let n = sequence.length;
155+
let m = word.length;
156+
for (let k = Math.floor(n / m); k > 0; k--) {
157+
if (sequence.includes(word.repeat(k))) {
158+
return k;
159+
}
160+
}
161+
return 0;
162+
}
163+
```
164+
165+
### **Rust**
166+
167+
```rust
168+
impl Solution {
169+
pub fn max_repeating(sequence: String, word: String) -> i32 {
170+
let n = sequence.len();
171+
let m = word.len();
172+
if n < m {
173+
return 0;
174+
}
175+
let mut dp = vec![0; n - m + 1];
176+
for i in 0..=n - m {
177+
let s = &sequence[i..i + m];
178+
if s == word {
179+
dp[i] = if (i as i32) - (m as i32) < 0 {
180+
0
181+
} else {
182+
dp[i - m]
183+
} + 1;
184+
}
185+
}
186+
*dp.iter().max().unwrap()
187+
}
188+
}
189+
```
190+
124191
### **...**
125192

126193
```

solution/1600-1699/1668.Maximum Repeating Substring/README_EN.md

+67
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,73 @@ func maxRepeating(sequence string, word string) int {
104104
}
105105
```
106106

107+
### **C**
108+
109+
```c
110+
#define max(a,b) (((a) > (b)) ? (a) : (b))
111+
112+
int findWord(int i, char *sequence, char *word) {
113+
int n = strlen(word);
114+
for (int j = 0; j < n; j++) {
115+
if (sequence[j + i] != word[j]) {
116+
return 0;
117+
}
118+
}
119+
return 1 + findWord(i + n, sequence, word);
120+
}
121+
122+
int maxRepeating(char *sequence, char *word) {
123+
int n = strlen(sequence);
124+
int m = strlen(word);
125+
int ans = 0;
126+
for (int i = 0; i <= n - m; i++) {
127+
ans = max(ans, findWord(i, sequence, word));
128+
}
129+
return ans;
130+
}
131+
```
132+
133+
### **TypeScript**
134+
135+
```ts
136+
function maxRepeating(sequence: string, word: string): number {
137+
let n = sequence.length;
138+
let m = word.length;
139+
for (let k = Math.floor(n / m); k > 0; k--) {
140+
if (sequence.includes(word.repeat(k))) {
141+
return k;
142+
}
143+
}
144+
return 0;
145+
}
146+
```
147+
148+
### **Rust**
149+
150+
```rust
151+
impl Solution {
152+
pub fn max_repeating(sequence: String, word: String) -> i32 {
153+
let n = sequence.len();
154+
let m = word.len();
155+
if n < m {
156+
return 0;
157+
}
158+
let mut dp = vec![0; n - m + 1];
159+
for i in 0..=n - m {
160+
let s = &sequence[i..i + m];
161+
if s == word {
162+
dp[i] = if (i as i32) - (m as i32) < 0 {
163+
0
164+
} else {
165+
dp[i - m]
166+
} + 1;
167+
}
168+
}
169+
*dp.iter().max().unwrap()
170+
}
171+
}
172+
```
173+
107174
### **...**
108175

109176
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#define max(a,b) (((a) > (b)) ? (a) : (b))
2+
3+
int findWord(int i, char *sequence, char *word) {
4+
int n = strlen(word);
5+
for (int j = 0; j < n; j++) {
6+
if (sequence[j + i] != word[j]) {
7+
return 0;
8+
}
9+
}
10+
return 1 + findWord(i + n, sequence, word);
11+
}
12+
13+
int maxRepeating(char *sequence, char *word) {
14+
int n = strlen(sequence);
15+
int m = strlen(word);
16+
int ans = 0;
17+
for (int i = 0; i <= n - m; i++) {
18+
ans = max(ans, findWord(i, sequence, word));
19+
}
20+
return ans;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn max_repeating(sequence: String, word: String) -> i32 {
3+
let n = sequence.len();
4+
let m = word.len();
5+
if n < m {
6+
return 0;
7+
}
8+
let mut dp = vec![0; n - m + 1];
9+
for i in 0..=n - m {
10+
let s = &sequence[i..i + m];
11+
if s == word {
12+
dp[i] = if (i as i32) - (m as i32) < 0 {
13+
0
14+
} else {
15+
dp[i - m]
16+
} + 1;
17+
}
18+
}
19+
*dp.iter().max().unwrap()
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function maxRepeating(sequence: string, word: string): number {
2+
let n = sequence.length;
3+
let m = word.length;
4+
for (let k = Math.floor(n / m); k > 0; k--) {
5+
if (sequence.includes(word.repeat(k))) {
6+
return k;
7+
}
8+
}
9+
return 0;
10+
}

0 commit comments

Comments
 (0)