forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveInvalidParentheses.cpp
112 lines (95 loc) · 3.29 KB
/
RemoveInvalidParentheses.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Source : https://leetcode.com/problems/remove-invalid-parentheses/
// Author : Hao Chen
// Date : 2015-11-11
/***************************************************************************************
*
* 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 ).
*
* Examples:
*
* "()())()" -> ["()()()", "(())()"]
* "(a)())()" -> ["(a)()()", "(a())()"]
* ")(" -> [""]
*
* Credits:Special thanks to @hpplayer for adding this problem and creating all test
* cases.
*
***************************************************************************************/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
using namespace std;
//DFS
void removeInvalidParenthesesHelper(string& s, int index, int pair,
int remove_left, int remove_right,
string solution, unordered_set<string>& result) {
char ch = s[index];
//recusive ending
if ( ch == '\0' ) {
if (pair==0 && remove_left==0 && remove_right==0 ) {
result.insert(solution);
}
return;
}
//other char, move to next one
if ( ch != '(' && ch != ')' ) {
removeInvalidParenthesesHelper(s, index+1, pair, remove_left, remove_right, solution+ch, result);
return;
}
//if we meet left one, and we need remove left one,
//then we have two choices : remove it, OR keep it.
if ( ch == '(' ) {
//revmoe it
if (remove_left > 0 ) {
removeInvalidParenthesesHelper(s, index+1, pair, remove_left-1, remove_right, solution, result);
}
//keep it
removeInvalidParenthesesHelper(s, index+1, pair+1, remove_left, remove_right, solution+ch, result);
}
//if we meet right one, and we need to remove right one,
//then we have two choices as well: remove it, or keep it if there are some left already.
if ( ch == ')' ) {
if (remove_right > 0 ) {
removeInvalidParenthesesHelper(s, index+1, pair, remove_left, remove_right-1, solution, result);
}
if (pair > 0){
removeInvalidParenthesesHelper(s, index+1, pair-1, remove_left, remove_right, solution+ch, result);
}
}
}
vector<string> removeInvalidParentheses(string s) {
//Calculating how many left/right parentheses need be removed!
int remove_left = 0, remove_right = 0;
for(auto c : s) {
if ( c == '(' ) {
remove_left++;
}else if ( c == ')' ){
if (remove_left > 0) {
remove_left--; // if we matched
}else{
remove_right++;
}
}
}
unordered_set<string> result;
removeInvalidParenthesesHelper(s, 0, 0, remove_left, remove_right, "", result);
return vector<string>( result.begin(), result.end() );
}
void printVector(vector<string> result) {
for( int i=0; i<result.size(); i++) {
cout << i << ") " << result[i] << endl;
}
}
int main(int argc, char** argv) {
string s = "()())()";
if (argc>1) {
s = argv[1];
}
cout << s << endl;
printVector( removeInvalidParentheses(s) );
return 0;
}