LeetCode solutions

4090. Minimum String Length After Balanced Removals

My accepted Python solution to LeetCode problem 4090, Minimum String Length After Balanced Removals, running in 4ms.

  • Difficulty: Medium
  • Python
  • Runtime 4ms
  • Memory 18.1MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4ms, memory 18.1MB, accepted 2025-12-30.

python
class Solution:
    def minLengthAfterRemovals(self, s: str) -> int:
        # We can remove any balanced substring
        # The minimum remaining is |count_a - count_b|
        # because we can always pair up a's and b's to remove
        count_a = s.count('a')
        count_b = s.count('b')
        return abs(count_a - count_b)

Source