Tags: "leetcode", "python", "dfs", "backtracking", access_time 1-min read

Edit this post on Github

Generate Parentheses

Created: July 22, 2019 by [lek-tin]

Last updated: July 22, 2019

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

Solution

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        """
        :type n: int
        :rtype: List[str]
        """
        ans = []
        self.dfs(ans, '', 0, 0, n)
        return ans

    def dfs(self, ans, S, left, right, n):
        if len(S) == 2 * n:
            ans.append(S)
            return

        if left < n:
            self.dfs(ans, S+'(', left+1, right, n)

        if right < left:
            self.dfs(ans, S+')', left, right+1, n)