328. Minimum Remove to Make Valid Parentheses

MediumStack

Given a string s of '(' , ')' and lowercase English characters, remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. A parentheses string is valid if and only if: it is the empty string, contains only lowercase characters, or it can be written as AB (A concatenated with B), where A and B are valid strings, or it can be written as (A), where A is a valid string.

Examples

Input: lee(t(c)o)de)

Output: lee(t(c)o)de

Explanation: Remove the trailing unmatched ')'.

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is either '(' , ')', or lowercase English letter.
Loading...

Run checks all cases above. Submit evaluates all test cases.