Skip to content

Commit e3d313f

Browse files
committed
feat: add solutinos to lc problem: No.1668
No.1668.Maximum Repeating Substring
1 parent 7160f57 commit e3d313f

File tree

10 files changed

+239
-274
lines changed

10 files changed

+239
-274
lines changed

solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,33 +106,6 @@ class Solution {
106106
}
107107
```
108108

109-
### **TypeScript**
110-
111-
```ts
112-
function minOperations(nums: number[], x: number): number {
113-
const total = nums.reduce((a, c) => a + c, 0);
114-
if (total < x) return -1;
115-
// 前缀和 + 哈希表, 求何为total - x的最长子序列
116-
const n = nums.length;
117-
const target = total - x;
118-
let hashMap = new Map();
119-
hashMap.set(0, -1);
120-
let pre = 0;
121-
let ans = -1;
122-
for (let right = 0; right < n; right++) {
123-
pre += nums[right];
124-
if (!hashMap.has(pre)) {
125-
hashMap.set(pre, right);
126-
}
127-
if (hashMap.has(pre - target)) {
128-
let left = hashMap.get(pre - target);
129-
ans = Math.max(right - left, ans);
130-
}
131-
}
132-
return ans == -1 ? -1 : n - ans;
133-
}
134-
```
135-
136109
### **C++**
137110

138111
```cpp
@@ -192,6 +165,67 @@ func min(a, b int) int {
192165
}
193166
```
194167

168+
### **TypeScript**
169+
170+
```ts
171+
function minOperations(nums: number[], x: number): number {
172+
const total = nums.reduce((a, c) => a + c, 0);
173+
if (total < x) return -1;
174+
// 前缀和 + 哈希表, 求何为total - x的最长子序列
175+
const n = nums.length;
176+
const target = total - x;
177+
let hashMap = new Map();
178+
hashMap.set(0, -1);
179+
let pre = 0;
180+
let ans = -1;
181+
for (let right = 0; right < n; right++) {
182+
pre += nums[right];
183+
if (!hashMap.has(pre)) {
184+
hashMap.set(pre, right);
185+
}
186+
if (hashMap.has(pre - target)) {
187+
let left = hashMap.get(pre - target);
188+
ans = Math.max(right - left, ans);
189+
}
190+
}
191+
return ans == -1 ? -1 : n - ans;
192+
}
193+
```
194+
195+
### **Rust**
196+
197+
```rust
198+
impl Solution {
199+
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
200+
let n = nums.len();
201+
let target = nums.iter().sum::<i32>() - x;
202+
203+
204+
let (mut l, mut r) = (0, 0);
205+
let (mut sum, mut max) = (0, -1);
206+
while r < n {
207+
sum += nums[r];
208+
r += 1;
209+
while sum > target && l < r {
210+
sum -= nums[l];
211+
l += 1;
212+
}
213+
214+
215+
if sum == target {
216+
max = max.max((r - l) as i32);
217+
}
218+
}
219+
220+
221+
if max == -1 {
222+
return max;
223+
}
224+
return n as i32 - max;
225+
}
226+
}
227+
```
228+
195229
### **...**
196230

197231
```

solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,6 @@ class Solution {
9292
}
9393
```
9494

95-
### **TypeScript**
96-
97-
```ts
98-
function minOperations(nums: number[], x: number): number {
99-
const total = nums.reduce((a, c) => a + c, 0);
100-
if (total < x) return -1;
101-
// 前缀和 + 哈希表, 求何为total - x的最长子序列
102-
const n = nums.length;
103-
const target = total - x;
104-
let hashMap = new Map();
105-
hashMap.set(0, -1);
106-
let pre = 0;
107-
let ans = -1;
108-
for (let right = 0; right < n; right++) {
109-
pre += nums[right];
110-
if (!hashMap.has(pre)) {
111-
hashMap.set(pre, right);
112-
}
113-
if (hashMap.has(pre - target)) {
114-
let left = hashMap.get(pre - target);
115-
ans = Math.max(right - left, ans);
116-
}
117-
}
118-
return ans == -1 ? -1 : n - ans;
119-
}
120-
```
121-
12295
### **C++**
12396

12497
```cpp
@@ -178,6 +151,66 @@ func min(a, b int) int {
178151
}
179152
```
180153

154+
### **TypeScript**
155+
156+
```ts
157+
function minOperations(nums: number[], x: number): number {
158+
const total = nums.reduce((a, c) => a + c, 0);
159+
if (total < x) return -1;
160+
const n = nums.length;
161+
const target = total - x;
162+
let hashMap = new Map();
163+
hashMap.set(0, -1);
164+
let pre = 0;
165+
let ans = -1;
166+
for (let right = 0; right < n; right++) {
167+
pre += nums[right];
168+
if (!hashMap.has(pre)) {
169+
hashMap.set(pre, right);
170+
}
171+
if (hashMap.has(pre - target)) {
172+
let left = hashMap.get(pre - target);
173+
ans = Math.max(right - left, ans);
174+
}
175+
}
176+
return ans == -1 ? -1 : n - ans;
177+
}
178+
```
179+
180+
### **Rust**
181+
182+
```rust
183+
impl Solution {
184+
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
185+
let n = nums.len();
186+
let target = nums.iter().sum::<i32>() - x;
187+
188+
189+
let (mut l, mut r) = (0, 0);
190+
let (mut sum, mut max) = (0, -1);
191+
while r < n {
192+
sum += nums[r];
193+
r += 1;
194+
while sum > target && l < r {
195+
sum -= nums[l];
196+
l += 1;
197+
}
198+
199+
200+
if sum == target {
201+
max = max.max((r - l) as i32);
202+
}
203+
}
204+
205+
206+
if max == -1 {
207+
return max;
208+
}
209+
return n as i32 - max;
210+
}
211+
}
212+
```
213+
181214
### **...**
182215

183216
```

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

Lines changed: 44 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -50,119 +50,77 @@
5050

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

53+
**方法一:暴力枚举**
54+
55+
直接从大到小枚举 `word` 的重复次数,判断 `word` 重复该次数后是否是 `sequence` 的子串即可。
56+
57+
时间复杂度为 $O(n^2)$,其中 $n$ 为 `sequence` 的长度。
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**
5662

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

5965
```python
60-
66+
class Solution:
67+
def maxRepeating(self, sequence: str, word: str) -> int:
68+
x = len(sequence) // len(word)
69+
for k in range(x, 0, -1):
70+
if word * k in sequence:
71+
return k
72+
return 0
6173
```
6274

6375
### **Java**
6476

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

6779
```java
68-
80+
class Solution {
81+
public int maxRepeating(String sequence, String word) {
82+
int x = sequence.length() / word.length();
83+
for (int k = x; k > 0; --k) {
84+
if (sequence.contains(word.repeat(k))) {
85+
return k;
86+
}
87+
}
88+
return 0;
89+
}
90+
}
6991
```
7092

7193
### **C++**
7294

7395
```cpp
7496
class Solution {
7597
public:
76-
int minOperations(vector<int>& nums, int x) {
77-
int n = nums.size();
78-
int sum = 0;
79-
for (int num : nums) {
80-
sum += num;
81-
}
82-
83-
int target = sum - x;
84-
int res = -1;
85-
int l = 0;
86-
int r = 0;
87-
sum = 0;
88-
while (r < n) {
89-
sum += nums[r++];
90-
while (sum > target && l < n) {
91-
sum -= nums[l++];
92-
}
93-
if (sum == target) {
94-
res = max(res, r - l);
98+
int maxRepeating(string sequence, string word) {
99+
int ans = 0;
100+
string t = word;
101+
int x = sequence.size() / word.size();
102+
for (int k = 1; k <= x; ++k) {
103+
if (sequence.find(t) != string::npos) {
104+
ans = k;
95105
}
106+
t += word;
96107
}
97-
98-
if (res == -1) {
99-
return res;
100-
}
101-
return n - res;
108+
return ans;
102109
}
103110
};
104111
```
105112
106-
### **TypeScript**
107-
108-
```ts
109-
function minOperations(nums: number[], x: number): number {
110-
const n = nums.length;
111-
const target = nums.reduce((r, v) => r + v) - x;
112-
113-
let l = 0;
114-
let r = 0;
115-
let sum = 0;
116-
let max = -1;
117-
while (r < n) {
118-
sum += nums[r++];
119-
while (sum > target && l < r) {
120-
sum -= nums[l++];
121-
}
122-
123-
if (sum === target) {
124-
max = Math.max(max, r - l);
125-
}
126-
}
127-
128-
if (max === -1) {
129-
return max;
130-
}
131-
return n - max;
132-
}
133-
```
134-
135-
### **Rust**
136-
137-
```rust
138-
impl Solution {
139-
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
140-
let n = nums.len();
141-
let target = nums.iter().sum::<i32>() - x;
142-
143-
144-
let (mut l, mut r) = (0, 0);
145-
let (mut sum, mut max) = (0, -1);
146-
while r < n {
147-
sum += nums[r];
148-
r += 1;
149-
while sum > target && l < r {
150-
sum -= nums[l];
151-
l += 1;
152-
}
153-
154-
155-
if sum == target {
156-
max = max.max((r - l) as i32);
157-
}
158-
}
159-
160-
161-
if max == -1 {
162-
return max;
163-
}
164-
return n as i32 - max;
165-
}
113+
### **Go**
114+
115+
```go
116+
func maxRepeating(sequence string, word string) int {
117+
x := len(sequence) / len(word)
118+
for k := x; k > 0; k-- {
119+
if strings.Contains(sequence, strings.Repeat(word, k)) {
120+
return k
121+
}
122+
}
123+
return 0
166124
}
167125
```
168126

0 commit comments

Comments
 (0)