301. Remove Invalid Parentheses
Problem description:
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: [“”]
Solution:
Question can ask in interview:
- what is invalid, if contains other characters or digits, will it be valid?
- Input type, string
- Output type, should be vector
- time/space complexity restriction
Solution:
We’re trying to make it valid, we need to remove a ‘)’. And should remove the extra parentheses by counting redundant
left or right parentheses. Then use dfs to check if the string is valid or not.
Check input is valid or not by using variable to count left parentheses and right parentheses.
count('(') >= count(')')
, i< n-1. Before reach to end of string, count of left parentheses should always be greater or equal than right parentheses. Because if right has more then left, it would be invalid.count('(') == count(')')
, i == n-1. When reach to the end, if it’s a valid string, number of left == number of right.Use dfs to check “if we remove extra parentheses, the string is valid or not”.
1 | class Solution: |
1 | class Solution { |
time complexity: $O(2 ^ {l+r})$
space complexity: $O((l+r)^2)$ ~ $O(n^2)$
reference:
https://goo.gl/KgYdwq
https://goo.gl/p61yqa
second time:
The same idea.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
60class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
/*
1. count how many invalid '(' or ')'
2. use dfs to get different output
isValid function
*/
vector<string> res;
stack<char> stk;
int i= 0;
int left= 0, right= 0;
while(i< s.length()){
if(s[i] == '(')
left++;
if(s[i] == ')'){
if(left== 0)
right++;
else
left--;
}
i++;
}
dfs(res, s, left, right, 0);
return res;
}
void dfs(vector<string>& res, string tmp, int left, int right, int start){
if(left == 0 && right == 0){
if(isValid(tmp))
res.push_back(tmp);
return;
}
for(int i= start; i< tmp.length(); i++){
//skip duplicate element
if(i != start && tmp[i] == tmp[i-1]) continue;
if(left>0 && tmp[i]== '(')
dfs(res, tmp.substr(0, i)+ tmp.substr(i+1), left-1, right, i);
if(right>0 && tmp[i] == ')')
dfs(res, tmp.substr(0, i)+ tmp.substr(i+1), left, right-1, i);
}
}
bool isValid(string s){
int left= 0, right= 0, i= 0;
while(i< s.length()){
if(s[i] == '(')
left++;
if(s[i] == ')')
left--;
if(left< 0) return false;
i++;
}
return left == 0;
}
};