LeetCode solutions

4019. Remove K-Balanced Substrings

My accepted Python solution to LeetCode problem 4019, Remove K-Balanced Substrings, running in 8955ms.

  • Difficulty: Medium
  • Python
  • Runtime 8955ms
  • Memory 18.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 8955ms, memory 18.2MB, accepted 2025-12-31.

python
class Solution:
    def removeSubstring(self, s: str, k: int) -> str:
        target = "(" * k + ")" * k
        while target in s:
            s = s.replace(target, "", 1)
        return s

Source