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

<p>Return <em>a list of <strong>unique strings</strong> that are valid with the minimum number of removals</em>. You may return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;()())()&quot;
<strong>Output:</strong> [&quot;(())()&quot;,&quot;()()()&quot;]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(a)())()&quot;
<strong>Output:</strong> [&quot;(a())()&quot;,&quot;(a)()()&quot;]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;)(&quot;
<strong>Output:</strong> [&quot;&quot;]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 25</code></li>
	<li><code>s</code> consists of lowercase English letters and parentheses <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code>.</li>
	<li>There will be at most <code>20</code> parentheses in <code>s</code>.</li>
</ul>

// backtrace + pruning

class Solution {
public:
    vector<string> ans = {};
    vector<int> leftNum;
    vector<int> rightNum;
    int minRemoval;

    void backtracing(string s, string temp, int i, int left, int right) {
        // cout << temp << endl;
        // cout << left << " " << right << endl;
        // ')' more than '(' in temp
        if (left < right)
            return;
        // add to ans
        if (left == right && (temp.size() == s.size() - minRemoval)) {
            string true_answer = temp;
            // cout << true_answer << endl;
            ans.push_back(true_answer);
            // for(int i = 0; i < ans.size(); i++){
            //     cout << ans[i] << ";  ";
            // } cout << endl;
        }
        // OOM
        if (i >= s.size())
            return;
        // prune if there are not enough ')' in the remaining string
        if (rightNum[i] < left - right)
            return;

        if (s[i] == '(') {
            temp += '(';
            backtracing(s, temp, i + 1, left + 1, right);
            temp = temp.substr(0, temp.size() - 1);
        } else if (s[i] == ')') {
            temp += ')';
            backtracing(s, temp, i + 1, left, right + 1);
            temp = temp.substr(0, temp.size() - 1);
        }
        // never remove non-parentheses
        else {
            temp += s[i];
            backtracing(s, temp, i + 1, left, right);
            return;
        }

        // remove s[i]
        backtracing(s, temp, i + 1, left, right);
    }
    vector<string> removeInvalidParentheses(string s) {
        int n = s.size();
        leftNum.resize(n);
        rightNum.resize(n);

        // find min removals
        int value = 0;
        int removal = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == '(') {
                value++;
            } else if (s[i] == ')') {
                if (value <= 0) {
                    removal++;
                } else {
                    value--;
                }
            }
        }
        removal += value;
        minRemoval = removal;

        // count leftNum and rightNum
        int left = 0;
        int right = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (s[i] == '(') {
                left++;
            } else if (s[i] == ')') {
                right++;
            }
            leftNum[i] = left;
            rightNum[i] = right;
        }

        string temp = "";

        // for (int i = 0; i < ans.size(); i++) {
        //     cout << ans[i] << ";  ";
        // }
        // cout << endl;

        backtracing(s, temp, 0, 0, 0);

        // cout << minRemoval << endl;
        // for(int i = 0; i < n; i++){
        //     cout << rightNum[i] << " ";
        // }

        // sort ans and remove duplicates
        // for(int i = 0; i < ans.size(); i++){
        //     cout << ans[i] << ";  ";
        // }
        // cout << endl;
        sort(ans.begin(), ans.end());
        for (int i = 0; i < ans.size(); i++) {
            for (int j = i + 1; j < ans.size(); j++) {
                if (ans[i] == ans[j]) {
                    ans.erase(ans.begin() + j);
                    j--;
                } else break;
            }
        }

        
        return ans;
    }
};
438211