<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:
    void combStep(int idx, string& curr, set<string>& res, string &s, int cutCount) {
        if (curr.size() == s.size() - cutCount) {
            if (isValid(curr)) {
                res.insert(curr);                
            }
            return;
        }
        for (int i = idx; i < s.size(); ++i) {
            curr.push_back(s[i]);            
            combStep(i+1, curr, res, s, cutCount);
            curr.pop_back();
        }
    }
    vector<string> removeInvalidParentheses(string s) {
        if (s.size() <= 1) {
            if(isValid(s)) {
                return {s};
            }
            return {""};
        }
        set<string> res;
        string curr = "";
        int cutCount = minRemove(s);
        combStep(0, curr, res, s, cutCount);
        return vector(res.begin(), res.end());
    }    
    int minRemove(string s) {
        stack<int> st;
        int invalidCount = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] != '(' && s[i] != ')') {
                ;
            }
            else if (s[i] == '(') {
                st.push(i);
            }
            else if (st.empty()) {
                invalidCount++;
            }
            else {
                st.pop();
            }
        }
        return st.size() + invalidCount;
    }    
    bool isValid(const string s) {
        stack<char> st;
        for (auto c: s) {
            //we don't care about anything other than ()
            if (c != '(' && c != ')') {
                continue;
            }
            else if (c == '(') {
                st.push(c);
            }
            else if (st.empty()) {
                return false;
            }
            else {
                st.pop();
            }
        }
        return st.empty();
    }
};

/*
class Solution {
public:
    void combStep(int idx, string& curr, set<string>& res, string &s) {
        if (idx > s.size()) {
            return;
        }
        else if (curr.size() == s.size()) {
            if (isValid(curr)) {
                res.insert(curr);                
            }
            return;
        }
        for (int i = idx; i < s.size(); ++i) {
            if (s[i] != '(' && s[i] != ')') {
                continue;
            }
            curr.push_back(s[i]);                
            combStep(i+1, curr, res, s);
            curr.pop_back();
        }
    }
    vector<string> removeInvalidParentheses(string s) {
        if (s.size() <= 1) {
            return {s};
        }
        s = makeValid(s);
        set<string> res;
        string curr = "";
        combStep(0, curr, res, s);
        return vector<string>(res.begin(), res.end());
    }    
    string makeValid(string s) {
        vector<bool> ommit(s.size(), false);
        stack<int> st;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] != '(' && s[i] != ')') {
                ;
            }
            else if (s[i] == '(') {
                st.push(i);
            }
            else {
                //)
                if (st.empty()) {
                    st.push(i);                    
                }
                else {
                    st.pop();
                }
            }
        }
        while(!st.empty()) {
            ommit[st.top()] = true;
            st.pop();
        }
        string res = "";
        for (int i = 0; i < s.size(); ++i) {
            if (ommit[i] == false) {
                res.push_back(s[i]);
            }
        }
        return res;
    }    
    bool isValid(const string s) {
        stack<char> st;
        for (auto c: s) {
            //we don't care about anything other than ()
            if (c != '(' && c != ')') {
                continue;
            }
            else if (c == '(') {
                st.push(c);
            }
            else if (st.empty()) {
                return false;
            }
            else {
                st.pop();
            }
        }
        return st.empty();
    }
};
*/
384901