<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 isValid(string &curr){
		bool ret = true;
		vector<char> st;
		for(int i=0; i<curr.length(); i++){
			if(curr[i] == '(' || curr[i] == '['){
				st.push_back(curr[i]);
			}else if(curr[i] == ')'){
				if(st.size() == 0 || st.back() != '(')
					return false;
				st.pop_back();
			}else if(curr[i] == ']'){
				if(st.size() == 0 || st.back() != '[')
					return false;
				st.pop_back();
			}
		}
		
		return (st.size() == 0);
	}
	void BT(string &s, int idx, string &curr, string &parenthesis, set<string>& sol){
		if(idx == s.length()){
			//cout << curr << endl;
			if(isValid(curr)){
				if(sol.size() == 0 || (*sol.begin()).length() == curr.length()){
					sol.insert(curr);
				}else if((*sol.begin()).length() < curr.length()){
					sol.clear();
					sol.insert(curr);
				}
			}
		}else{
			if( s[idx] == '(' || s[idx] == '['){
				/*add and try*/
				parenthesis.push_back(s[idx]);
				curr.push_back(s[idx]);
				BT(s, idx+1, curr, parenthesis, sol);
				curr.pop_back();
				parenthesis.pop_back();
				
				/*do not add and try*/
				BT(s, idx+1, curr, parenthesis, sol);
			}else if( s[idx] == ')'){
				if((parenthesis.size() > 0 &&  parenthesis.back() == '(' )){
					parenthesis.pop_back();
					curr.push_back(s[idx]);
					BT(s, idx+1, curr, parenthesis, sol);
					curr.pop_back();
					parenthesis.push_back('(');
				}
				
				/*do not add and try*/
				BT(s, idx+1, curr, parenthesis, sol);
			}else if( s[idx] == ']'){
				if((parenthesis.size() > 0 &&  parenthesis.back() == '[' )){
					parenthesis.pop_back();
					curr.push_back(s[idx]);
					BT(s, idx+1, curr, parenthesis, sol);
					curr.pop_back();
					parenthesis.push_back('[');
				}
				
				/*do not add and try*/
				BT(s, idx+1, curr, parenthesis, sol);
			}else if(s[idx] >= 'a' || s[idx] <= 'z'){;
				curr.push_back(s[idx]);
				BT(s, idx+1, curr, parenthesis, sol);
				curr.pop_back();
			}
		}
	}
    vector<string> removeInvalidParentheses(string s) {
       set<string> sol;
	   string curr;
	   string parenthesis;
	   vector<string> ret;
	   BT(s, 0, curr, parenthesis, sol);
	   
	   for(auto i: sol){
		ret.push_back(i);
	   }
	   
	   return ret;
    }
};
431548