3746. Minimum String Length After Balanced Removals Medium
1/**
2 * [3746] Minimum String Length After Balanced Removals
3 *
4 * You are given a string s consisting only of the characters 'a' and 'b'.
5 * You are allowed to repeatedly remove any <span data-keyword="substring-nonempty">substring</span> where the number of 'a' characters is equal to the number of 'b' characters. After each removal, the remaining parts of the string are concatenated together without gaps.
6 * Return an integer denoting the minimum possible length of the string after performing any number of such operations.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "aabbab"</span>
11 * Output: <span class="example-io">0</span>
12 * Explanation:
13 * The substring "aabbab" has three 'a' and three 'b'. Since their counts are equal, we can remove the entire string directly. The minimum length is 0.
14 * </div>
15 * <strong class="example">Example 2:
16 * <div class="example-block">
17 * Input: <span class="example-io">s = "aaaa"</span>
18 * Output: <span class="example-io">4</span>
19 * Explanation:
20 * Every substring of "aaaa" contains only 'a' characters. No substring can be removed as a result, so the minimum length remains 4.
21 * </div>
22 * <strong class="example">Example 3:
23 * <div class="example-block">
24 * Input: <span class="example-io">s = "aaabb"</span>
25 * Output: <span class="example-io">1</span>
26 * Explanation:
27 * First, remove the substring "ab", leaving "aab". Next, remove the new substring "ab", leaving "a". No further removals are possible, so the minimum length is 1.
28 * </div>
29 *
30 * Constraints:
31 *
32 * 1 <= s.length <= 10^5
33 * s[i] is either 'a' or 'b'.
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/minimum-string-length-after-balanced-removals/
39// discuss: https://leetcode.com/problems/minimum-string-length-after-balanced-removals/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn min_length_after_removals(s: String) -> i32 {
45 0
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_3746() {
57 }
58}
59Back
© 2026 bowen.ge All Rights Reserved.