301.Remove-Invalid-Parentheses

301. Remove Invalid Parentheses

题目地址

https://leetcode.com/problems/remove-invalid-parentheses/

题目描述

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

代码

Approach #1 BFS

Time: T(n) = n x C(n, n) + (n-1) x C(n, n-1) + ... + 1 x C(n, 1) = n x 2^(n-1)

Approach #2 DFS

Time: O(2^n) Space: O(n)

Approach #3 Backtracing

Last updated

Was this helpful?