<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>

class Solution {
public:
    vector<string> removeInvalidParentheses(string s) {
        
        int total = 0;
        for (char c : s) total += (c == '(' || c == ')');
        
        int removals = minimumRemovals(s);
        
        if (removals == 0) return {s};
        
        unordered_set<string> res;
        
        for (int mask = 0; mask < (1 << total); mask++) {
            if (__builtin_popcount(mask) != removals) continue;
            string t = build(s, mask);
            if (t.size() > 0) res.insert(t);
        }
        
        if (res.size() == 0) return {""};
        
        return vector<string>(res.begin(), res.end());
    }
    
    string build(string& s, int mask) {
        string res;
        int i = 0;
        stack<char> stack;
        for (char c : s) {
            if (c != '(' && c != ')') {
                res += c;
                continue;
            } else {
                if (mask & (1 << i)) {
                    i++;
                    continue;
                }
                i++;
                res += c;
                if (c == ')') {
                    if (stack.size() && stack.top() == '(') stack.pop();
                    else stack.push(c);
                } else {
                    stack.push(c);
                }
            }
        }
        return stack.size() == 0 ? res : "";
    }
    
    int minimumRemovals(string& s) {
        stack<char> stack;
        for (char c : s) {
            if (c != '(' && c != ')')
                continue;
            if (c == ')') {
                if (stack.size() && stack.top() == '(') {
                    stack.pop();
                } else {
                    stack.push(c);
                }
            } else {
                stack.push(c);
            }
        }
        return stack.size();
    }
};
298273