Skip to content

Commit 0f1860d

Browse files
committed
feat: add solutions to lc problem: No.1653
No.1653.Minimum Deletions to Make String Balanced
1 parent 777a2a7 commit 0f1860d

File tree

6 files changed

+350
-0
lines changed

6 files changed

+350
-0
lines changed

solution/1600-1699/1653.Minimum Deletions to Make String Balanced/README.md

+154
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,176 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:动态规划**
49+
50+
定义 `dp[i]` 表示字符串 `s[0..i)` 达到平衡时,需要删除的最少字符数,答案为 `dp[n]`。用变量 $b$ 统计字符 `b` 的个数。
51+
52+
遍历字符串 $s$:
53+
54+
如果当前字符为 `b`,此时不影响平衡串的最小删除次数,那么 $dp[i]=dp[i-1]$,并且累加 $b$。
55+
56+
如果当前字符为 `a`,此时,要想达到平衡,要么把前面的所有 `b` 都删掉,操作次数为 $b$;要么把当前的 `a` 删除,操作次数为 $dp[i-1]+1$。取两者的最小值即可。即 $dp[i]=\min(b,dp[i-1]+1)$。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
59+
60+
我们发现 $dp[i]$ 只与 $dp[i-1]$ 有关,因此可以使用变量 `ans` 来代替数组 `dp`。空间复杂度优化为 $O(1)$。
61+
4862
<!-- tabs:start -->
4963

5064
### **Python3**
5165

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

5468
```python
69+
class Solution:
70+
def minimumDeletions(self, s: str) -> int:
71+
n = len(s)
72+
dp = [0] * (n + 1)
73+
b = 0
74+
for i, c in enumerate(s, 1):
75+
if c == 'b':
76+
dp[i] = dp[i - 1]
77+
b += 1
78+
else:
79+
dp[i] = min(dp[i - 1] + 1, b)
80+
return dp[n]
81+
```
5582

83+
```python
84+
class Solution:
85+
def minimumDeletions(self, s: str) -> int:
86+
ans = b = 0
87+
for c in s:
88+
if c == 'b':
89+
b += 1
90+
else:
91+
ans = min(b, ans + 1)
92+
return ans
5693
```
5794

5895
### **Java**
5996

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

6299
```java
100+
class Solution {
101+
public int minimumDeletions(String s) {
102+
int n = s.length();
103+
int[] dp = new int[n + 1];
104+
int b = 0;
105+
for (int i = 0; i < n; ++i) {
106+
if (s.charAt(i) == 'b') {
107+
dp[i + 1] = dp[i];
108+
++b;
109+
} else {
110+
dp[i + 1] = Math.min(dp[i] + 1, b);
111+
}
112+
}
113+
return dp[n];
114+
}
115+
}
116+
```
117+
118+
```java
119+
class Solution {
120+
public int minimumDeletions(String s) {
121+
int ans = 0, b = 0;
122+
for (char c : s.toCharArray()) {
123+
if (c == 'b') {
124+
++b;
125+
} else {
126+
ans = Math.min(b, ans + 1);
127+
}
128+
}
129+
return ans;
130+
}
131+
}
132+
```
133+
134+
### **C++**
135+
136+
```cpp
137+
class Solution {
138+
public:
139+
int minimumDeletions(string s) {
140+
int n = s.size();
141+
vector<int> dp(n + 1);
142+
int b = 0;
143+
for (int i = 0; i < n; ++i) {
144+
if (s[i] == 'b') {
145+
dp[i + 1] = dp[i];
146+
++b;
147+
} else {
148+
dp[i + 1] = min(dp[i] + 1, b);
149+
}
150+
}
151+
return dp[n];
152+
}
153+
};
154+
```
155+
156+
```cpp
157+
class Solution {
158+
public:
159+
int minimumDeletions(string s) {
160+
int ans = 0, b = 0;
161+
for (char c : s) {
162+
if (c == 'b') {
163+
++b;
164+
} else {
165+
ans = min(b, ans + 1);
166+
}
167+
}
168+
return ans;
169+
}
170+
};
171+
```
172+
173+
### **Go**
174+
175+
```go
176+
func minimumDeletions(s string) int {
177+
n := len(s)
178+
dp := make([]int, n+1)
179+
b := 0
180+
for i, c := range s {
181+
if c == 'b' {
182+
b++
183+
dp[i+1] = dp[i]
184+
} else {
185+
dp[i+1] = min(dp[i]+1, b)
186+
}
187+
}
188+
return dp[n]
189+
}
190+
191+
func min(a, b int) int {
192+
if a < b {
193+
return a
194+
}
195+
return b
196+
}
197+
```
63198

199+
```go
200+
func minimumDeletions(s string) int {
201+
ans, b := 0, 0
202+
for _, c := range s {
203+
if c == 'b' {
204+
b++
205+
} else {
206+
ans = min(b, ans+1)
207+
}
208+
}
209+
return ans
210+
}
211+
212+
func min(a, b int) int {
213+
if a < b {
214+
return a
215+
}
216+
return b
217+
}
64218
```
65219

66220
### **...**

solution/1600-1699/1653.Minimum Deletions to Make String Balanced/README_EN.md

+142
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,160 @@ Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u
3939

4040
## Solutions
4141

42+
Dynamic programming.
43+
4244
<!-- tabs:start -->
4345

4446
### **Python3**
4547

4648
```python
49+
class Solution:
50+
def minimumDeletions(self, s: str) -> int:
51+
n = len(s)
52+
dp = [0] * (n + 1)
53+
b = 0
54+
for i, c in enumerate(s, 1):
55+
if c == 'b':
56+
dp[i] = dp[i - 1]
57+
b += 1
58+
else:
59+
dp[i] = min(dp[i - 1] + 1, b)
60+
return dp[n]
61+
```
4762

63+
```python
64+
class Solution:
65+
def minimumDeletions(self, s: str) -> int:
66+
ans = b = 0
67+
for c in s:
68+
if c == 'b':
69+
b += 1
70+
else:
71+
ans = min(b, ans + 1)
72+
return ans
4873
```
4974

5075
### **Java**
5176

5277
```java
78+
class Solution {
79+
public int minimumDeletions(String s) {
80+
int n = s.length();
81+
int[] dp = new int[n + 1];
82+
int b = 0;
83+
for (int i = 0; i < n; ++i) {
84+
if (s.charAt(i) == 'b') {
85+
dp[i + 1] = dp[i];
86+
++b;
87+
} else {
88+
dp[i + 1] = Math.min(dp[i] + 1, b);
89+
}
90+
}
91+
return dp[n];
92+
}
93+
}
94+
```
95+
96+
```java
97+
class Solution {
98+
public int minimumDeletions(String s) {
99+
int ans = 0, b = 0;
100+
for (char c : s.toCharArray()) {
101+
if (c == 'b') {
102+
++b;
103+
} else {
104+
ans = Math.min(b, ans + 1);
105+
}
106+
}
107+
return ans;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
int minimumDeletions(string s) {
118+
int n = s.size();
119+
vector<int> dp(n + 1);
120+
int b = 0;
121+
for (int i = 0; i < n; ++i) {
122+
if (s[i] == 'b') {
123+
dp[i + 1] = dp[i];
124+
++b;
125+
} else {
126+
dp[i + 1] = min(dp[i] + 1, b);
127+
}
128+
}
129+
return dp[n];
130+
}
131+
};
132+
```
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
int minimumDeletions(string s) {
138+
int ans = 0, b = 0;
139+
for (char c : s) {
140+
if (c == 'b') {
141+
++b;
142+
} else {
143+
ans = min(b, ans + 1);
144+
}
145+
}
146+
return ans;
147+
}
148+
};
149+
```
150+
151+
### **Go**
152+
153+
```go
154+
func minimumDeletions(s string) int {
155+
n := len(s)
156+
dp := make([]int, n+1)
157+
b := 0
158+
for i, c := range s {
159+
if c == 'b' {
160+
b++
161+
dp[i+1] = dp[i]
162+
} else {
163+
dp[i+1] = min(dp[i]+1, b)
164+
}
165+
}
166+
return dp[n]
167+
}
168+
169+
func min(a, b int) int {
170+
if a < b {
171+
return a
172+
}
173+
return b
174+
}
175+
```
53176

177+
```go
178+
func minimumDeletions(s string) int {
179+
ans, b := 0, 0
180+
for _, c := range s {
181+
if c == 'b' {
182+
b++
183+
} else {
184+
ans = min(b, ans+1)
185+
}
186+
}
187+
return ans
188+
}
189+
190+
func min(a, b int) int {
191+
if a < b {
192+
return a
193+
}
194+
return b
195+
}
54196
```
55197

56198
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int minimumDeletions(string s) {
4+
int ans = 0, b = 0;
5+
for (char c : s) {
6+
if (c == 'b') {
7+
++b;
8+
} else {
9+
ans = min(b, ans + 1);
10+
}
11+
}
12+
return ans;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func minimumDeletions(s string) int {
2+
ans, b := 0, 0
3+
for _, c := range s {
4+
if c == 'b' {
5+
b++
6+
} else {
7+
ans = min(b, ans+1)
8+
}
9+
}
10+
return ans
11+
}
12+
13+
func min(a, b int) int {
14+
if a < b {
15+
return a
16+
}
17+
return b
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int minimumDeletions(String s) {
3+
int ans = 0, b = 0;
4+
for (char c : s.toCharArray()) {
5+
if (c == 'b') {
6+
++b;
7+
} else {
8+
ans = Math.min(b, ans + 1);
9+
}
10+
}
11+
return ans;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minimumDeletions(self, s: str) -> int:
3+
ans = b = 0
4+
for c in s:
5+
if c == 'b':
6+
b += 1
7+
else:
8+
ans = min(b, ans + 1)
9+
return ans

0 commit comments

Comments
 (0)