Remove Invalid Parentheses Problem

Description

LeetCode Problem 301.

Given a string s that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid.

Return all the possible results. You may return the answer in any order.

Example 1:

``````1
2
Input: s = "()())()"
Output: ["(())()","()()()"]
``````

Example 2:

``````1
2
Input: s = "(a)())()"
Output: ["(a())()","(a)()()"]
``````

Example 3:

``````1
2
Input: s = ")("
Output: [""]
``````

Constraints:

• 1 <= s.length <= 25
• s consists of lowercase English letters and parentheses ‘(‘ and ‘)’.
• There will be at most 20 parentheses in s.

Sample C++ Code

``````1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution {
public:
set<string> paren;

bool checkValid(string s) {
stack<char> st;
for (int i = 0; i < s.size(); i ++) {
if (s[i] == '(')
st.push(s[i]);
else if (s[i] == ')') {
if (st.empty())
return false;
st.pop();
} else {
continue;
}
}
if (st.empty())
return true;
else
return false;
}

void dfs(string s, int idx, int l) {

if (s.size() == l) {
bool flag = checkValid(s);
if (flag)
paren.insert(s);
return;
}
if (idx > l || s.size() < l)
return;

if (s[idx] != '(' && s[idx] != ')')
dfs(s, idx+1, l);
else {
dfs(s, idx+1, l);

string ns = s.substr(0, idx)+s.substr(idx+1, s.size()-idx-1);
dfs(ns, idx, l);
}

}

vector<string> removeInvalidParentheses(string s) {
stack<char> st;
for (int i = 0; i < s.size(); i ++) {
if (s[i] == '(')
st.push(s[i]);
else if (s[i] == ')') {
if (!st.empty() && st.top() == '(')
st.pop();
else
st.push(s[i]);
}
}

int l = s.size() - st.size();

dfs(s, 0, l);

vector<string> ans;
for (auto x : paren)
ans.push_back(x);
return ans;
}
};
``````