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

from collections import deque
class Solution:
    def removeInvalidParentheses(self, s: str) -> List[str]:
        
        #Iteration 
        def is_valid(string):
            
            count = 0
            for i in range(len(string)):
                if string[i] == "(":
                    count += 1
                if string[i] == ")":
                    count -= 1
                if count < 0:
                    return False
            
            return count == 0
        
        q = deque()
        q.append(s)
        res = []
        visited = set()
        valid_found = False
        maxlen = -1
        
        while q:
            st = q.popleft()
            
            if is_valid(st) :
                if len(st) > maxlen:
                    res = []
                    maxlen = len(st)
                    res.append(st)
                elif len(st) == maxlen:
                    res.append(st)
                #visited.add(st)
                valid_found = True 
            
            if valid_found:
                continue 
                
            for i in range(len(st)):
                if st[:i]+st[i+1:] not in visited:
                    q.append( st[:i]+st[i+1:]  )
                    visited.add(st[:i]+st[i+1:])
            
        
        return res
        


        """
        #Recursion with back tracking 
        def dfs(st, curr_ind,res_set,left_count,right_count ):
             
            
            if curr_ind >= len(st):
                if left_count == right_count:
                    #print("res_set", res_set, "max_len", max_len)

                    if len(res_set) > self.max_len:
                        self.res = set()
                        self.res.add( "".join(res_set) )
                        self.max_len = len(res_set)
                    elif len(res_set) == self.max_len:
                        self.res.add( "".join(res_set) )
                
                    #print("res", self.res, "max_len", self.max_len)
                
                    
            else:
                curr_str = st[curr_ind]
                
                if curr_str == "(":
                    #Consider the paranthesis
                    res_set.append(curr_str)
                    dfs(st,curr_ind+1, res_set,left_count+1, right_count)
                    #backtrack
                    res_set.pop()
                    
                    #Ignore paranthesis
                    dfs(st,curr_ind+1, res_set,left_count, right_count)
                    
                elif curr_str == ")":
                    
                    #Ignore paranthesis
                    dfs(st,curr_ind+1, res_set,left_count, right_count)
                    
                    #Consider only if left_count is  > right_count :
                    if left_count > right_count:
                        res_set.append(curr_str)
                        dfs(st,curr_ind+1, res_set,left_count, right_count+1)
                        #backtrack
                        res_set.pop()
                    
                else:
                    #If char is neither '(' nor ')'
                    res_set.append(curr_str)
                    dfs(st,curr_ind+1, res_set,left_count, right_count)
                    res_set.pop()
        
        
        self.max_len = -float("inf")
        self.res = set()
        
        
        dfs(s,0,[],0,0)
        
        return self.res
        """
18600