Remove Invalid Parentheses(dfs,bfs)

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: 
[""]

分析

DFS:

得到多余的rmL和rmR。然后dfs,记得加一个左就Open+,右就 -- 用set去重

string + char : path += s[pos]

string - char: path = path[:-1]

BFS

filter('()'.count,s) 这里count对()都会是1,但是字母就是0,所以滤掉了字母

q是set

Last updated

Was this helpful?