|
| 1 | +// Source : https://leetcode.com/problems/remove-invalid-parentheses/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2015-11-11 |
| 4 | + |
| 5 | +/*************************************************************************************** |
| 6 | + * |
| 7 | + * Remove the minimum number of invalid parentheses in order to make the input string |
| 8 | + * valid. Return all possible results. |
| 9 | + * |
| 10 | + * Note: The input string may contain letters other than the parentheses ( and ). |
| 11 | + * |
| 12 | + * Examples: |
| 13 | + * |
| 14 | + * "()())()" -> ["()()()", "(())()"] |
| 15 | + * "(a)())()" -> ["(a)()()", "(a())()"] |
| 16 | + * ")(" -> [""] |
| 17 | + * |
| 18 | + * Credits:Special thanks to @hpplayer for adding this problem and creating all test |
| 19 | + * cases. |
| 20 | + * |
| 21 | + ***************************************************************************************/ |
| 22 | + |
| 23 | +#include <iostream> |
| 24 | +#include <vector> |
| 25 | +#include <string> |
| 26 | +#include <unordered_set> |
| 27 | +using namespace std; |
| 28 | + |
| 29 | +//DFS |
| 30 | +void removeInvalidParenthesesHelper(string& s, int index, int pair, |
| 31 | + int remove_left, int remove_right, |
| 32 | + string solution, unordered_set<string>& result) { |
| 33 | + |
| 34 | + char ch = s[index]; |
| 35 | + |
| 36 | + //recusive ending |
| 37 | + if ( ch == '\0' ) { |
| 38 | + if (pair==0 && remove_left==0 && remove_right==0 ) { |
| 39 | + result.insert(solution); |
| 40 | + } |
| 41 | + return; |
| 42 | + } |
| 43 | + //other char, move to next one |
| 44 | + if ( ch != '(' && ch != ')' ) { |
| 45 | + removeInvalidParenthesesHelper(s, index+1, pair, remove_left, remove_right, solution+ch, result); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + //if we meet left one, and we need remove left one, |
| 50 | + //then we have two choices : remove it, OR keep it. |
| 51 | + if ( ch == '(' ) { |
| 52 | + //revmoe it |
| 53 | + if (remove_left > 0 ) { |
| 54 | + removeInvalidParenthesesHelper(s, index+1, pair, remove_left-1, remove_right, solution, result); |
| 55 | + } |
| 56 | + //keep it |
| 57 | + removeInvalidParenthesesHelper(s, index+1, pair+1, remove_left, remove_right, solution+ch, result); |
| 58 | + } |
| 59 | + |
| 60 | + //if we meet right one, and we need to remove right one, |
| 61 | + //then we have two choices as well: remove it, or keep it if there are some left already. |
| 62 | + if ( ch == ')' ) { |
| 63 | + if (remove_right > 0 ) { |
| 64 | + removeInvalidParenthesesHelper(s, index+1, pair, remove_left, remove_right-1, solution, result); |
| 65 | + } |
| 66 | + if (pair > 0){ |
| 67 | + removeInvalidParenthesesHelper(s, index+1, pair-1, remove_left, remove_right, solution+ch, result); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +vector<string> removeInvalidParentheses(string s) { |
| 75 | + |
| 76 | + //Calculating how many left/right parentheses need be removed! |
| 77 | + int remove_left = 0, remove_right = 0; |
| 78 | + for(auto c : s) { |
| 79 | + if ( c == '(' ) { |
| 80 | + remove_left++; |
| 81 | + }else if ( c == ')' ){ |
| 82 | + if (remove_left > 0) { |
| 83 | + remove_left--; // if we matched |
| 84 | + }else{ |
| 85 | + remove_right++; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + unordered_set<string> result; |
| 91 | + removeInvalidParenthesesHelper(s, 0, 0, remove_left, remove_right, "", result); |
| 92 | + return vector<string>( result.begin(), result.end() ); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +void printVector(vector<string> result) { |
| 97 | + for( int i=0; i<result.size(); i++) { |
| 98 | + cout << i << ") " << result[i] << endl; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +int main(int argc, char** argv) { |
| 104 | + string s = "()())()"; |
| 105 | + if (argc>1) { |
| 106 | + s = argv[1]; |
| 107 | + } |
| 108 | + cout << s << endl; |
| 109 | + printVector( removeInvalidParentheses(s) ); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
0 commit comments