Skip to content

Commit e476261

Browse files
committed
feat: add solutions to lc problem: No.1750
No.1750.Minimum Length of String After Deleting Similar Ends
1 parent 13d4f5a commit e476261

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README.md

+69
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,75 @@ func max(a, b int) int {
157157
}
158158
```
159159

160+
### **TypeScript**
161+
162+
```ts
163+
function minimumLength(s: string): number {
164+
const n = s.length;
165+
let start = 0;
166+
let end = n - 1;
167+
while (start < end && s[start] === s[end]) {
168+
while (start + 1 < end && s[start] === s[start + 1]) {
169+
start++;
170+
}
171+
while (start < end - 1 && s[end] === s[end - 1]) {
172+
end--;
173+
}
174+
start++;
175+
end--;
176+
}
177+
return Math.max(0, end - start + 1);
178+
}
179+
```
180+
181+
### **Rust**
182+
183+
```rust
184+
impl Solution {
185+
pub fn minimum_length(s: String) -> i32 {
186+
let s = s.as_bytes();
187+
let n = s.len();
188+
let mut start = 0;
189+
let mut end = n - 1;
190+
while start < end && s[start] == s[end] {
191+
while start + 1 < end && s[start] == s[start + 1] {
192+
start += 1;
193+
}
194+
while start < end - 1 && s[end] == s[end - 1] {
195+
end -= 1;
196+
}
197+
start += 1;
198+
end -= 1;
199+
}
200+
0.max(end - start + 1) as i32
201+
}
202+
}
203+
```
204+
205+
### **C**
206+
207+
```c
208+
int minimumLength(char *s) {
209+
int n = strlen(s);
210+
int start = 0;
211+
int end = n - 1;
212+
while (start < end && s[start] == s[end]) {
213+
while (start + 1 < end && s[start] == s[start + 1]) {
214+
start++;
215+
}
216+
while (start < end - 1 && s[end] == s[end - 1]) {
217+
end--;
218+
}
219+
start++;
220+
end--;
221+
}
222+
if (start > end) {
223+
return 0;
224+
}
225+
return end - start + 1;
226+
}
227+
```
228+
160229
### **...**
161230
162231
```

solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README_EN.md

+69
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,75 @@ func max(a, b int) int {
141141
}
142142
```
143143

144+
### **TypeScript**
145+
146+
```ts
147+
function minimumLength(s: string): number {
148+
const n = s.length;
149+
let start = 0;
150+
let end = n - 1;
151+
while (start < end && s[start] === s[end]) {
152+
while (start + 1 < end && s[start] === s[start + 1]) {
153+
start++;
154+
}
155+
while (start < end - 1 && s[end] === s[end - 1]) {
156+
end--;
157+
}
158+
start++;
159+
end--;
160+
}
161+
return Math.max(0, end - start + 1);
162+
}
163+
```
164+
165+
### **Rust**
166+
167+
```rust
168+
impl Solution {
169+
pub fn minimum_length(s: String) -> i32 {
170+
let s = s.as_bytes();
171+
let n = s.len();
172+
let mut start = 0;
173+
let mut end = n - 1;
174+
while start < end && s[start] == s[end] {
175+
while start + 1 < end && s[start] == s[start + 1] {
176+
start += 1;
177+
}
178+
while start < end - 1 && s[end] == s[end - 1] {
179+
end -= 1;
180+
}
181+
start += 1;
182+
end -= 1;
183+
}
184+
0.max(end - start + 1) as i32
185+
}
186+
}
187+
```
188+
189+
### **C**
190+
191+
```c
192+
int minimumLength(char *s) {
193+
int n = strlen(s);
194+
int start = 0;
195+
int end = n - 1;
196+
while (start < end && s[start] == s[end]) {
197+
while (start + 1 < end && s[start] == s[start + 1]) {
198+
start++;
199+
}
200+
while (start < end - 1 && s[end] == s[end - 1]) {
201+
end--;
202+
}
203+
start++;
204+
end--;
205+
}
206+
if (start > end) {
207+
return 0;
208+
}
209+
return end - start + 1;
210+
}
211+
```
212+
144213
### **...**
145214
146215
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
int minimumLength(char *s) {
2+
int n = strlen(s);
3+
int start = 0;
4+
int end = n - 1;
5+
while (start < end && s[start] == s[end]) {
6+
while (start + 1 < end && s[start] == s[start + 1]) {
7+
start++;
8+
}
9+
while (start < end - 1 && s[end] == s[end - 1]) {
10+
end--;
11+
}
12+
start++;
13+
end--;
14+
}
15+
if (start > end) {
16+
return 0;
17+
}
18+
return end - start + 1;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn minimum_length(s: String) -> i32 {
3+
let s = s.as_bytes();
4+
let n = s.len();
5+
let mut start = 0;
6+
let mut end = n - 1;
7+
while start < end && s[start] == s[end] {
8+
while start + 1 < end && s[start] == s[start + 1] {
9+
start += 1;
10+
}
11+
while start < end - 1 && s[end] == s[end - 1] {
12+
end -= 1;
13+
}
14+
start += 1;
15+
end -= 1;
16+
}
17+
0.max(end - start + 1) as i32
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function minimumLength(s: string): number {
2+
const n = s.length;
3+
let start = 0;
4+
let end = n - 1;
5+
while (start < end && s[start] === s[end]) {
6+
while (start + 1 < end && s[start] === s[start + 1]) {
7+
start++;
8+
}
9+
while (start < end - 1 && s[end] === s[end - 1]) {
10+
end--;
11+
}
12+
start++;
13+
end--;
14+
}
15+
return Math.max(0, end - start + 1);
16+
}

0 commit comments

Comments
 (0)