Skip to content

Latest commit

 

History

History
51 lines (36 loc) · 1.22 KB

_22. Generate Parentheses.md

File metadata and controls

51 lines (36 loc) · 1.22 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 13, 2024

Last updated : July 01, 2024


Related Topics : String, Dynamic Programming, Backtracking

Acceptance Rate : 76.15 %


Solutions

Python

class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        output = []

        def helper(curr: [], openLeft: int, closeLeft: int) -> None :
            if closeLeft == 0 == openLeft :
                output.append(''.join(curr))
                return

            if openLeft > 0 :
                curr.append('(')
                helper(curr, openLeft - 1, closeLeft)
                curr.pop()

            if closeLeft <= openLeft :
                return

            curr.append(')')
            helper(curr, openLeft, closeLeft - 1)
            curr.pop()

        helper([], n, n)
        return output