[LeetCode] Minimum String Length After Balanced Removals

3746. Minimum String Length After Balanced Removals

You are given a string s consisting only of the characters 'a' and 'b'.

You are allowed to repeatedly remove any substring 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.

Return an integer denoting the minimum possible length of the string after performing any number of such operations.

1
2
3
4
5
6
class Solution {
public:
int minLengthAfterRemovals(string s) {
return abs((int)s.length() - count(begin(s), end(s), 'a') * 2);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/21/PS/LeetCode/minimum-string-length-after-balanced-removals/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.