Python slow but accepted


  • 0
    C

    """
    class Solution(object):
    def findMinStep(self, A, T):
    """
    :type board: str
    :type hand: str
    :rtype: int
    """

        T=sorted(T)
        res=[]
        res2=[A,[]]
        self.done=0
        self.dfs2(A,[],T,res)
        return min([len(r) for r in res]) if res else -1  
        
              
                         
     
     
    def check(self,s):
            res=[]
            new=""
            for i in range(0,len(s)-2):
                if len(set(s[i:i+3]))==1:
                    j=i
                    while j<len(s):
                        if s[i]==s[j]:
                            res.extend([j])
                            j=j+1
                        else:
                            break
            
            for i in range(len(s)):
                if i in res:
                    continue
                else:
                    new=new+s[i]
     
       
            return new   
    
    def bigcheck(self,st):           
        
        
        while True:
            
            nst=self.check(st)
            
            if len(nst)==len(st):
                break
            
            st=nst
            
        return nst
    
    
                
                        
                        
    
                
    def dfs2(self,st,curr,put,res):
    
        if len(st)==0:
           
            
            return True
        
        if len(put)==0:
            return
        
        for i in range(len(st)):
            for j in range(len(put)):
                
                if j and put[j]==put[j-1]:
                    continue
                
                
                nst=self.bigcheck(st[:i]+put[j]+st[i:])
                if len(nst)<len(st)+1 and self.dfs2(nst,curr+[put[j]],put[:j]+put[j+1:],res):
                    
                    res.append(curr+[put[j]])
                    self.done=1
                    return
    
        
                        
                
                elif self.done==0:
                    if j<len(put)-1:
                        if put[j]!=put[j+1]:
                            continue
                        nst1=self.bigcheck(st[:i]+put[j]+put[j+1]+st[i:])
                    
                        if len(nst1)<len(st)+1 and self.dfs2(nst1,curr+[put[j]]+[put[j+1]],put[:j]+put[j+2:],res):
                            res.append(curr+[put[j]]+[put[j+1]])
                            return
               
        return False
    

    """


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.