|  | 
|  | 1 | +/* | 
|  | 2 | + * Remove the minimum number of invalid parentheses in order to make the input string valid. | 
|  | 3 | + * Return all possible results. | 
|  | 4 | + * Note: The input string may contain letters other than the parentheses ( and ). | 
|  | 5 | + *  | 
|  | 6 | + * Source: Leetcode, 301. Remove Invalid Parentheses | 
|  | 7 | + *  | 
|  | 8 | + * solution approach: | 
|  | 9 | + *  | 
|  | 10 | + * We can solve this problem in many ways, I am choosing DFS. | 
|  | 11 | + * For a string to be valid, we have two requirements: | 
|  | 12 | + * 1. The number of left and right parenthesis should be same. | 
|  | 13 | + * 2. They should be in right order, for each open there has to be a close, so for example '()' is valid | 
|  | 14 | + * but ')(' is not. | 
|  | 15 | + *  | 
|  | 16 | + * 1. We track the count of left and right parenthesis which are out of place. | 
|  | 17 | + * 2. We recursively backtrack and go two ways. | 
|  | 18 | + * If a character is left or right parenthesis, | 
|  | 19 | + *  a.  we keep it | 
|  | 20 | + *  b. or we drop it. | 
|  | 21 | + * 3. In both the cases we adjust open left and right parameters accordingly. | 
|  | 22 | + * Open is the count of parenthesis sets which are open but not close yet. | 
|  | 23 | + * When we reach end of string, we check if it is valid by left, right and open count and | 
|  | 24 | + * push the string to result set. | 
|  | 25 | + * 4. If a character is not a parenthesis operator, we simply add it to the result string. | 
|  | 26 | + * 5. At the end of backtracking, we simply replace the current result string with the one | 
|  | 27 | + * we started this iteration with. | 
|  | 28 | + *  | 
|  | 29 | + * We are using a set to keep the results unique. | 
|  | 30 | + *  | 
|  | 31 | + */ | 
|  | 32 | + | 
|  | 33 | + | 
|  | 34 | +#include <iostream> | 
|  | 35 | +#include <string> | 
|  | 36 | +#include <unordered_set> | 
|  | 37 | +#include <vector> | 
|  | 38 | + | 
|  | 39 | +void dfs(const std::string& s, std::string temp, int index, int left, int right, int open, | 
|  | 40 | +            std::unordered_set<std::string>& result) | 
|  | 41 | +{ | 
|  | 42 | +    if (index == s.length()) { | 
|  | 43 | +        if (left == 0 && right == 0 && open == 0) { | 
|  | 44 | +            result.insert(temp); | 
|  | 45 | +        } | 
|  | 46 | +        return; | 
|  | 47 | +    } | 
|  | 48 | +     | 
|  | 49 | +    if (left < 0 || right < 0 || open < 0) { | 
|  | 50 | +        return; | 
|  | 51 | +    } | 
|  | 52 | +     | 
|  | 53 | +    std::string current{temp}; | 
|  | 54 | +    char c = s[index]; | 
|  | 55 | +    if (c == '(') { | 
|  | 56 | +        // Drop | 
|  | 57 | +        dfs(s, temp, index + 1, left - 1, right, open, result); | 
|  | 58 | +        // Keep | 
|  | 59 | +        dfs(s, temp + c, index + 1, left, right, open + 1, result); | 
|  | 60 | +    } else if (c == ')') { | 
|  | 61 | +        // Drop | 
|  | 62 | +        dfs(s, temp, index + 1, left, right - 1, open, result); | 
|  | 63 | +        // Keep | 
|  | 64 | +        dfs(s, temp + c, index + 1, left, right, open - 1, result); | 
|  | 65 | +    } else { | 
|  | 66 | +        dfs(s, temp + c, index + 1, left, right, open, result); | 
|  | 67 | +    } | 
|  | 68 | +     | 
|  | 69 | +    temp = current; | 
|  | 70 | +} | 
|  | 71 | + | 
|  | 72 | +std::vector<std::string> remove_invalid_parenthesis(const std::string& s) { | 
|  | 73 | +    std::vector<std::string> result; | 
|  | 74 | +    if (s.length() == 0) { | 
|  | 75 | +        result.push_back(""); | 
|  | 76 | +        return result; | 
|  | 77 | +    } | 
|  | 78 | +    int left = 0; | 
|  | 79 | +    int right = 0; | 
|  | 80 | +    for (char c : s) { | 
|  | 81 | +        if (c == '(') left++; | 
|  | 82 | +        else if (c == ')') { | 
|  | 83 | +            if (left > 0) left--; | 
|  | 84 | +            else right++; | 
|  | 85 | +        } | 
|  | 86 | +    } | 
|  | 87 | +    std::unordered_set<std::string> result_set; | 
|  | 88 | +    std::string temp(""); | 
|  | 89 | +    dfs(s, temp, 0, left, right, 0, result_set); | 
|  | 90 | +    return std::vector<std::string>(result_set.begin(), result_set.end()); | 
|  | 91 | +} | 
|  | 92 | + | 
|  | 93 | +template<typename T> | 
|  | 94 | +void print_vec(const std::vector<T>& vec) { | 
|  | 95 | +    for (auto v: vec) { | 
|  | 96 | +        std::cout << v << " "; | 
|  | 97 | +    } | 
|  | 98 | +    std::cout << std::endl; | 
|  | 99 | +} | 
|  | 100 | + | 
|  | 101 | +int main() | 
|  | 102 | +{ | 
|  | 103 | +    std::string str("(a)())()"); | 
|  | 104 | +    std::cout << "Pattern: " << str << std::endl; | 
|  | 105 | +    std::cout << "Possible valid parenthesis combinations: "; | 
|  | 106 | +    print_vec(remove_invalid_parenthesis(str)); | 
|  | 107 | +} | 
0 commit comments