#1249
Minimum Remove to Make Valid Parentheses
pupil · 535 · lc medium +28 · verified · 71.3% accepted · 7,374 likes · top 80%
Description
You are given a string s containing '(', ')', and lowercase English letters. Remove the fewest parentheses possible so that the remaining string has a valid parenthesis structure, and return any valid result.
A parenthesis string is valid if and only if:
- It is empty or contains only lowercase letters, or
- It can be written as AB where A and B are both valid, or
- It can be written as (A) where A is valid.
Example 1:
Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
Example 2:
Input: s = "a)b(c)d"
Output: "ab(c)d"
Example 3:
Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.
Code
1
2
3