Practice/Meta/Leetcode 1249. Minimum Remove to Make Valid Parentheses
CodingMust
You are given a string containing lowercase English letters along with opening and closing parentheses. Your task is to remove the minimum number of parentheses (either opening '(' or closing ')') to make the resulting string valid.
A valid parentheses string is one where:
Return any valid result. If multiple solutions exist with the same minimum number of removals, any one is acceptable.
Example 1:
Input: s = "ab(c)d)e(f" Output: "ab(c)def" Explanation: We remove the unmatched ')' after 'd' and the unmatched '(' before 'f'. The substring "(c)" remains properly matched.
Example 2:
Input: s = "))((" Output: "" Explanation: All brackets are unmatched. Removing all of them gives an empty string, which is valid.
Example 3:
Input: s = "((a)b(c))" Output: "((a)b(c))" Explanation: All brackets are already properly matched, so no removal is necessary.
Example 4:
Input: s = "x(y(z))" Output: "x(y(z))" Explanation: The brackets form a valid nested structure, so we keep everything.