301. Remvoe Invalid Parenthesis

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.

Note: The input string may contain letters other than the parentheses(and).

Example 1:

Input:
 "()())()"

Output:
 ["()()()", "(())()"]

Example 2:

Input:
 "(a)())()"

Output:
 ["(a)()()", "(a())()"]

Example 3:

Input:")("

Output: [""]

Facebook Variation: any solution that results in minimum removal of parentheses

Thoughts: 楼主用了一个3-pass的方法: 先从头到尾找close比open多的,移掉; 从尾到头找open比close多的,移掉; 然后最后一遍build

Thoughts:

  1. Expanding the set and test whether each combination is valid, once there is answer in the set , then return (we want the minimum edition)

Code: Python

Last updated

Was this helpful?