puzzling result: the output is wizard. who could explain it for me,thanks.


  • 0
    M

    this is the result.
    (screenshit

    Notice that the same input gets different outputs when submitting and testing.

    this is my code

    ans = set()
    class Solution(object):
        def removeInvalidParentheses(self, s):
            """
            :type s: str
            :rtype: List[str]
            """
            if s=="":return ['']
            self.li = list(s)
            self.init()
            return list(ans)
        def isGood(self,s):
            left = right = 0
            for i in s:
                if i=='(':
                    left+= 1
                elif i==')':
                    right+=1
                if left<right: return False
            if left != right:return False
            return True
        def init(self):
            left = right = 0
            l_index = []
            r_index = []
            for idx,i in enumerate(self.li):
                if i == '(':
                    l_index.append(idx)
                    left +=1
                elif i == ")":
                    r_index.append(idx)
                    right +=1
            n = abs(left-right)
            if left > right:
                self.deal(l_index,n,[])
            else:self.deal(r_index,n,[])
            
        def deal(self,index,n,select):
            if n == 0:
                select.sort(reverse=True)
                li = list(self.li)
                for i in select:
                    del li[i]
                if self.isGood(li):
                    ans.add(''.join(li))
                return 
            for j,i in enumerate(index):
                idxs = select + [i]
                tmp = list(index)
                del tmp[j]
                self.deal(tmp,n-1,idxs)
    

Log in to reply
 

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