Python


  • 0
    T

    Based on this: https://stackoverflow.com/a/3172190/3606244, I can't really take credit

    class Solution:
        def generateParenthesis(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            result = set()
            self._generate(n, 0, "", result)
            return list(result)
            
            
        def _generate(self, openCount, closeCount, s, result):
            if openCount == 0 and closeCount == 0:
                result.add(s)
            if openCount > 0:
                self._generate(openCount-1, closeCount+1, s + "(", result)
            if closeCount > 0:
                self._generate(openCount, closeCount-1, s + ")", result)
    

Log in to reply
 

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