Python: Easy to Read Solution


  • 0
    P
          def generateParenthesis(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            if n < 2:
                return ["()"*n]
        
            solset = set()
            prevcomb = self.generateParenthesis(n-1)
            n = len(prevcomb[0])
            for e in prevcomb:
                base = "(" + e
                for i in range(n):
                    solset.add(base[:i+1] + ")" + base[i+1:])
            return list(solset)
    

Log in to reply
 

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