Skip to content

Commit 7c3a6d7

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

File tree

6 files changed

+213
-2
lines changed

6 files changed

+213
-2
lines changed

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

+78-1
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,99 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:双指针**
66+
67+
我们定义两个指针 $i$ 和 $j$ 分别指向字符串 $s$ 的头部和尾部,然后向中间移动,直到 $i$ 和 $j$ 指向的字符不相等,此时 $\max(0, j - i + 1)$ 即为答案。
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
70+
6571
<!-- tabs:start -->
6672

6773
### **Python3**
6874

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

7177
```python
72-
78+
class Solution:
79+
def minimumLength(self, s: str) -> int:
80+
i, j = 0, len(s) - 1
81+
while i < j and s[i] == s[j]:
82+
while i + 1 < j and s[i] == s[i + 1]:
83+
i += 1
84+
while i < j - 1 and s[j - 1] == s[j]:
85+
j -= 1
86+
i, j = i + 1, j - 1
87+
return max(0, j - i + 1)
7388
```
7489

7590
### **Java**
7691

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

7994
```java
95+
class Solution {
96+
public int minimumLength(String s) {
97+
int i = 0, j = s.length() - 1;
98+
while (i < j && s.charAt(i) == s.charAt(j)) {
99+
while (i + 1 < j && s.charAt(i) == s.charAt(i + 1)) {
100+
++i;
101+
}
102+
while (i < j - 1 && s.charAt(j) == s.charAt(j - 1)) {
103+
--j;
104+
}
105+
++i;
106+
--j;
107+
}
108+
return Math.max(0, j - i + 1);
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
int minimumLength(string s) {
119+
int i = 0, j = s.size() - 1;
120+
while (i < j && s[i] == s[j]) {
121+
while (i + 1 < j && s[i] == s[i + 1]) {
122+
++i;
123+
}
124+
while (i < j - 1 && s[j] == s[j - 1]) {
125+
--j;
126+
}
127+
++i;
128+
--j;
129+
}
130+
return max(0, j - i + 1);
131+
}
132+
};
133+
```
80134
135+
### **Go**
136+
137+
```go
138+
func minimumLength(s string) int {
139+
i, j := 0, len(s)-1
140+
for i < j && s[i] == s[j] {
141+
for i+1 < j && s[i] == s[i+1] {
142+
i++
143+
}
144+
for i < j-1 && s[j] == s[j-1] {
145+
j--
146+
}
147+
i, j = i+1, j-1
148+
}
149+
return max(0, j-i+1)
150+
}
151+
152+
func max(a, b int) int {
153+
if a > b {
154+
return a
155+
}
156+
return b
157+
}
81158
```
82159

83160
### **...**

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

+72-1
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,84 @@
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def minimumLength(self, s: str) -> int:
66+
i, j = 0, len(s) - 1
67+
while i < j and s[i] == s[j]:
68+
while i + 1 < j and s[i] == s[i + 1]:
69+
i += 1
70+
while i < j - 1 and s[j - 1] == s[j]:
71+
j -= 1
72+
i, j = i + 1, j - 1
73+
return max(0, j - i + 1)
6574
```
6675

6776
### **Java**
6877

6978
```java
79+
class Solution {
80+
public int minimumLength(String s) {
81+
int i = 0, j = s.length() - 1;
82+
while (i < j && s.charAt(i) == s.charAt(j)) {
83+
while (i + 1 < j && s.charAt(i) == s.charAt(i + 1)) {
84+
++i;
85+
}
86+
while (i < j - 1 && s.charAt(j) == s.charAt(j - 1)) {
87+
--j;
88+
}
89+
++i;
90+
--j;
91+
}
92+
return Math.max(0, j - i + 1);
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int minimumLength(string s) {
103+
int i = 0, j = s.size() - 1;
104+
while (i < j && s[i] == s[j]) {
105+
while (i + 1 < j && s[i] == s[i + 1]) {
106+
++i;
107+
}
108+
while (i < j - 1 && s[j] == s[j - 1]) {
109+
--j;
110+
}
111+
++i;
112+
--j;
113+
}
114+
return max(0, j - i + 1);
115+
}
116+
};
117+
```
70118
119+
### **Go**
120+
121+
```go
122+
func minimumLength(s string) int {
123+
i, j := 0, len(s)-1
124+
for i < j && s[i] == s[j] {
125+
for i+1 < j && s[i] == s[i+1] {
126+
i++
127+
}
128+
for i < j-1 && s[j] == s[j-1] {
129+
j--
130+
}
131+
i, j = i+1, j-1
132+
}
133+
return max(0, j-i+1)
134+
}
135+
136+
func max(a, b int) int {
137+
if a > b {
138+
return a
139+
}
140+
return b
141+
}
71142
```
72143

73144
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int minimumLength(string s) {
4+
int i = 0, j = s.size() - 1;
5+
while (i < j && s[i] == s[j]) {
6+
while (i + 1 < j && s[i] == s[i + 1]) {
7+
++i;
8+
}
9+
while (i < j - 1 && s[j] == s[j - 1]) {
10+
--j;
11+
}
12+
++i;
13+
--j;
14+
}
15+
return max(0, j - i + 1);
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func minimumLength(s string) int {
2+
i, j := 0, len(s)-1
3+
for i < j && s[i] == s[j] {
4+
for i+1 < j && s[i] == s[i+1] {
5+
i++
6+
}
7+
for i < j-1 && s[j] == s[j-1] {
8+
j--
9+
}
10+
i, j = i+1, j-1
11+
}
12+
return max(0, j-i+1)
13+
}
14+
15+
func max(a, b int) int {
16+
if a > b {
17+
return a
18+
}
19+
return b
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int minimumLength(String s) {
3+
int i = 0, j = s.length() - 1;
4+
while (i < j && s.charAt(i) == s.charAt(j)) {
5+
while (i + 1 < j && s.charAt(i) == s.charAt(i + 1)) {
6+
++i;
7+
}
8+
while (i < j - 1 && s.charAt(j) == s.charAt(j - 1)) {
9+
--j;
10+
}
11+
++i;
12+
--j;
13+
}
14+
return Math.max(0, j - i + 1);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def minimumLength(self, s: str) -> int:
3+
i, j = 0, len(s) - 1
4+
while i < j and s[i] == s[j]:
5+
while i + 1 < j and s[i] == s[i + 1]:
6+
i += 1
7+
while i < j - 1 and s[j - 1] == s[j]:
8+
j -= 1
9+
i, j = i + 1, j - 1
10+
return max(0, j - i + 1)

0 commit comments

Comments
 (0)