Remove K-Balanced Substrings
medium · 32.5% accepted · 128 likes · top 10%
string · stack · simulation
Description
You are given a string s consisting of '(' and ')', and an integer k.
A string is k-balanced if it is exactly k consecutive '(' followed by k consecutive ')', i.e., '(' * k + ')' * k.
For example, if k = 3, k-balanced is "((()))".
You must repeatedly remove all non-overlapping k-balanced substrings from s, and then join the remaining parts. Continue this process until no k-balanced substring exists.
Return the final string after all possible removals.
Example 1:
Input: s = "(())", k = 1
Output: ""
Explanation:
k-balanced substring is "()"
Step
Current s
k-balanced
Result s
1
(())
(())
()
2
()
()
Empty
Thus, the final string is "".
Solution