Minimum Remove to Make Valid Parentheses
MediumStack
Decode it first. Clues, constraints, pattern and plan before any code.You get a string s of lowercase letters and the brackets ( and ). Remove the minimum number of brackets (from any positions) so that the brackets left are balanced and properly nested, and return the resulting string. Letters always stay, and the remaining characters keep their order.
Several answers may be equally short. Return any valid answer: the tests check that your string is balanced, that it is s with only brackets removed, and that it is as long as possible.
s = "lee(t(c)o)de)" → "lee(t(c)o)de" ("lee(t(co)de)" is also fine)
s = "a)b(c)d" → "ab(c)d"
s = "))((" → ""Constraints
1 <= s.length <= 10^5- Each
s[i]is(,), or a lowercase English letter.