Skip to content

Commit 9421323

Browse files
committed
+ problem 1653
1 parent b6ce503 commit 9421323

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 1653. Minimum Deletions to Make String Balanced
2+
You are given a string `s` consisting only of characters `'a'` and `'b'`.
3+
4+
You can delete any number of characters in `s` to make `s` **balanced**. `s` is **balanced** if there is no pair of indices `(i,j)` such that `i < j` and `s[i] = 'b'` and `s[j]= 'a'`.
5+
6+
Return *the **minimum** number of deletions needed to make* `s` ***balanced***.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> s = "aababbab"
11+
<strong>Output:</strong> 2
12+
<strong>Explanation:</strong> You can either:
13+
Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
14+
Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> s = "bbaaaaabb"
20+
<strong>Output:</strong> 2
21+
<strong>Explanation:</strong> The only solution is to delete the first two characters.
22+
</pre>
23+
24+
#### Constraints:
25+
* <code>1 <= s.length <= 10<sup>5</sup></code>
26+
* `s[i]` is `'a'` or `'b'`.
27+
28+
## Solutions (Rust)
29+
30+
### 1. Solution
31+
```Rust
32+
impl Solution {
33+
pub fn minimum_deletions(s: String) -> i32 {
34+
let mut count = s.chars().filter(|&c| c == 'a').count() as i32;
35+
let mut ret = count;
36+
37+
for c in s.chars() {
38+
count += (c == 'b') as i32 - (c == 'a') as i32;
39+
ret = ret.min(count);
40+
}
41+
42+
ret
43+
}
44+
}
45+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 1653. 使字符串平衡的最少删除次数
2+
给你一个字符串 `s` ,它仅包含字符 `'a'``'b'`
3+
4+
你可以删除 `s` 中任意数目的字符,使得 `s` **平衡** 。当不存在下标对 `(i,j)` 满足 `i < j` ,且 `s[i] = 'b'` 的同时 `s[j]= 'a'` ,此时认为 `s`**平衡** 的。
5+
6+
请你返回使 `s` **平衡****最少** 删除次数。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> s = "aababbab"
11+
<strong>输出:</strong> 2
12+
<strong>解释:</strong> 你可以选择以下任意一种方案:
13+
下标从 0 开始,删除第 2 和第 6 个字符("aababbab" -> "aaabbb"),
14+
下标从 0 开始,删除第 3 和第 6 个字符("aababbab" -> "aabbbb")。
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> s = "bbaaaaabb"
20+
<strong>输出:</strong> 2
21+
<strong>解释:</strong> 唯一的最优解是删除最前面两个字符。
22+
</pre>
23+
24+
#### 提示:
25+
* <code>1 <= s.length <= 10<sup>5</sup></code>
26+
* `s[i]` 要么是 `'a'` 要么是 `'b'`
27+
28+
## 题解 (Rust)
29+
30+
### 1. 题解
31+
```Rust
32+
impl Solution {
33+
pub fn minimum_deletions(s: String) -> i32 {
34+
let mut count = s.chars().filter(|&c| c == 'a').count() as i32;
35+
let mut ret = count;
36+
37+
for c in s.chars() {
38+
count += (c == 'b') as i32 - (c == 'a') as i32;
39+
ret = ret.min(count);
40+
}
41+
42+
ret
43+
}
44+
}
45+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn minimum_deletions(s: String) -> i32 {
3+
let mut count = s.chars().filter(|&c| c == 'a').count() as i32;
4+
let mut ret = count;
5+
6+
for c in s.chars() {
7+
count += (c == 'b') as i32 - (c == 'a') as i32;
8+
ret = ret.min(count);
9+
}
10+
11+
ret
12+
}
13+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@
874874
[1647][1647l]|[Minimum Deletions to Make Character Frequencies Unique][1647] |![rb]&nbsp;&nbsp;![rs]
875875
[1648][1648l]|[Sell Diminishing-Valued Colored Balls][1648] |![rs]
876876
[1652][1652l]|[Defuse the Bomb][1652] |![rb]&nbsp;&nbsp;![rs]
877+
[1653][1653l]|[Minimum Deletions to Make String Balanced][1653] |![rs]
877878
[1656][1656l]|[Design an Ordered Stream][1656] |![py]
878879
[1658][1658l]|[Minimum Operations to Reduce X to Zero][1658] |![rb]&nbsp;&nbsp;![rs]
879880
[1662][1662l]|[Check If Two String Arrays are Equivalent][1662] |![rb]&nbsp;&nbsp;![rs]
@@ -2170,6 +2171,7 @@
21702171
[1647]:Problemset/1647-Minimum%20Deletions%20to%20Make%20Character%20Frequencies%20Unique/README.md#1647-minimum-deletions-to-make-character-frequencies-unique
21712172
[1648]:Problemset/1648-Sell%20Diminishing-Valued%20Colored%20Balls/README.md#1648-sell-diminishing-valued-colored-balls
21722173
[1652]:Problemset/1652-Defuse%20the%20Bomb/README.md#1652-defuse-the-bomb
2174+
[1653]:Problemset/1653-Minimum%20Deletions%20to%20Make%20String%20Balanced/README.md#1653-minimum-deletions-to-make-string-balanced
21732175
[1656]:Problemset/1656-Design%20an%20Ordered%20Stream/README.md#1656-design-an-ordered-stream
21742176
[1658]:Problemset/1658-Minimum%20Operations%20to%20Reduce%20X%20to%20Zero/README.md#1658-minimum-operations-to-reduce-x-to-zero
21752177
[1662]:Problemset/1662-Check%20If%20Two%20String%20Arrays%20are%20Equivalent/README.md#1662-check-if-two-string-arrays-are-equivalent
@@ -3469,6 +3471,7 @@
34693471
[1647l]:https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/
34703472
[1648l]:https://leetcode.com/problems/sell-diminishing-valued-colored-balls/
34713473
[1652l]:https://leetcode.com/problems/defuse-the-bomb/
3474+
[1653l]:https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/
34723475
[1656l]:https://leetcode.com/problems/design-an-ordered-stream/
34733476
[1658l]:https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/
34743477
[1662l]:https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@
874874
[1647][1647l]|[字符频次唯一的最小删除次数][1647] |![rb]&nbsp;&nbsp;![rs]
875875
[1648][1648l]|[销售价值减少的颜色球][1648] |![rs]
876876
[1652][1652l]|[拆炸弹][1652] |![rb]&nbsp;&nbsp;![rs]
877+
[1653][1653l]|[使字符串平衡的最少删除次数][1653] |![rs]
877878
[1656][1656l]|[设计有序流][1656] |![py]
878879
[1658][1658l]|[将 x 减到 0 的最小操作数][1658] |![rb]&nbsp;&nbsp;![rs]
879880
[1662][1662l]|[检查两个字符串数组是否相等][1662] |![rb]&nbsp;&nbsp;![rs]
@@ -2170,6 +2171,7 @@
21702171
[1647]:Problemset/1647-Minimum%20Deletions%20to%20Make%20Character%20Frequencies%20Unique/README_CN.md#1647-字符频次唯一的最小删除次数
21712172
[1648]:Problemset/1648-Sell%20Diminishing-Valued%20Colored%20Balls/README_CN.md#1648-销售价值减少的颜色球
21722173
[1652]:Problemset/1652-Defuse%20the%20Bomb/README_CN.md#1652-拆炸弹
2174+
[1653]:Problemset/1653-Minimum%20Deletions%20to%20Make%20String%20Balanced/README_CN.md#1653-使字符串平衡的最少删除次数
21732175
[1656]:Problemset/1656-Design%20an%20Ordered%20Stream/README_CN.md#1656-设计有序流
21742176
[1658]:Problemset/1658-Minimum%20Operations%20to%20Reduce%20X%20to%20Zero/README_CN.md#1658-将-x-减到-0-的最小操作数
21752177
[1662]:Problemset/1662-Check%20If%20Two%20String%20Arrays%20are%20Equivalent/README_CN.md#1662-检查两个字符串数组是否相等
@@ -3469,6 +3471,7 @@
34693471
[1647l]:https://leetcode.cn/problems/minimum-deletions-to-make-character-frequencies-unique/
34703472
[1648l]:https://leetcode.cn/problems/sell-diminishing-valued-colored-balls/
34713473
[1652l]:https://leetcode.cn/problems/defuse-the-bomb/
3474+
[1653l]:https://leetcode.cn/problems/minimum-deletions-to-make-string-balanced/
34723475
[1656l]:https://leetcode.cn/problems/design-an-ordered-stream/
34733476
[1658l]:https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/
34743477
[1662l]:https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/

0 commit comments

Comments
 (0)