<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:
    int max_string_size;
    set<string> result;
    vector <int> closed_p;
    
    vector<string> removeInvalidParentheses(string s) {
        max_string_size = 0;
        
        int open = 0;
        int closed = 0;
        int char_count = 0;
       
        for(int i = 0; i < s.size(); ++i){
            closed_p.push_back(0);
             if(s[i] == '(' ){
                 open += 1;
             } else if(s[i] == ')'){
                 if(closed + 1 <= open){
                    closed += 1; 
                 }
                 
             } else {
                 char_count += 1;
             }
            //cout << open << " : " << closed << " : " << char_count << endl;
        }
        
        int prev = 0;
        for(int i = s.size()-1; i >= 0; --i){
            
            int closed_plus = s[i] == ')' ? 1 : 0;
            closed_p[i] = prev + closed_plus;
            prev = closed_p[i];
        }
        
        
        
        max_string_size = 2 * closed + char_count;
        //cout << max_string_size << endl;
        
        bt(s, 0, 0, 0, "");
        
        return vector<string>(result.begin(), result.end());
    }
    
    
    //s = "()())()"
    //idx: 7
    //s[idx]= (
    //partial:()
    //o:1  c:
    //res: ["()"]
    void bt(string s, int idx, int open, int closed, string partial){

        if(idx >= s.size()){
            if(open == closed){
                // if(partial.size() > max_string_size){
                //     result.clear();
                //     result.insert(partial);
                //     max_string_size = partial.size();
                // } else 
                if(partial.size() == max_string_size){
                    result.insert(partial);
                }
            }
            return;
        }
        
        if((s.size() - idx) + partial.size() < max_string_size){
            return;
        }
        
        //cout << idx << ": " << "o: " << open << " vs " << closed_p[idx] << endl;
        if(open > closed_p[idx] + closed){
            //cout << "exiting due to not enough closed p" << endl;
            return;
        }
        //"((((((((((((((((((((aaaaa"
        
        if(isalpha(s[idx])){
            partial += s[idx];
            bt(s, idx+1, open, closed, partial);
        } else {
            
            //don't add
            bt(s, idx+1, open, closed, partial);
            
            //add
            int p_size = partial.size();
            partial += s[idx];
            
            if(s[idx] == '(' ){
                bt(s, idx+1, open+1, closed, partial);
            } else if( closed + 1 <= open){
                bt(s, idx+1, open, closed+1, partial);
            }
            
            partial = partial.substr(0, p_size);
        }
    }
};
45050