Minimum Remove to Make Valid Parentheses
Given a string s of '(' , ')' and lowercase English letters, remove the minimum number of parentheses ('(' or ')') so that the resulting string is valid and return any such result. A string is valid if it is empty, contains only letters, or every parenthesis is properly matched. Letters are never removed.
Open official problem prompt ↗Produce a valid string by deleting as few parentheses as possible while leaving letters and their positions intact.
Like proofreading brackets in an equation: cross out every stray closing bracket that opens nothing, then cross out every opening bracket that was never closed.
- Input
- s = "a)b(c)d"
- Output
- "ab(c)d"
- Why
- The leading ')' at index 1 has no matching '(', so removing just that one closing parenthesis makes the string valid while keeping all letters and the matched pair.
1 <= s.length <= 10^5s[i] is either '(', ')', or a lowercase English letter