<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:
    bool check(string& s){
        stack<char>s1;
        for(int i=0;i<s.size();i++){
            if(s[i]==')'){
                while(s1.size() && s1.top()!='('){
                    s1.pop();
                }
                if(s1.size() && s1.top()=='('){
                    s1.pop();
                }
                else{
                    return false;
                }
            }
            else{
                s1.push(s[i]);
            }
        }
        while(s1.size()){
            if(s1.top()=='(' || s1.top()==')'){
                return false;
            }
            s1.pop();
        }
        return true;
        
    }
    void ans(int i,unordered_set<string>&ans1,string& curr,string& s,int cnt){
        if(i==s.size()){
            if(curr.size()==cnt && check(curr)){
                ans1.insert(curr);
            }
            return ;
        }
        curr.push_back(s[i]);
        ans(i+1,ans1,curr,s,cnt);
        curr.pop_back();
        if(s[i]=='(' || s[i]==')'){
        ans(i+1,ans1,curr,s,cnt);
        }
    }
    vector<string> removeInvalidParentheses(string s) {
        int cnt=0;
        stack<char>s2;
        for(int i=0;i<s.size();i++){
            if(s[i]==')'){
                while(s2.size() && s2.top()!='('){
                    s2.pop();
                }
                if(s2.size() && s2.top()=='('){
                    s2.pop();
                }
                else{
                    cnt++;
                }
            }
            else{
                s2.push(s[i]);
            }
        }
        while(s2.size()){
            if(s2.top()=='('){
                cnt++;
            }
            s2.pop();
        }
        // cout<<cnt<<endl;
        cnt=s.size()-cnt;
        unordered_set<string>ans1;
        string curr="";
        ans(0,ans1,curr,s,cnt);
        vector<string>fin;
        for(auto x:ans1){
            fin.push_back(x);
        }
        return fin;
    }
};
311600