๐Ÿ“ฆ schubart / LeetCode

๐Ÿ“„ lc_0022.py ยท 20 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20from typing import List


# noinspection PyMethodMayBeStatic,PyPep8Naming
class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        def generate(opening: int, closing: int) -> List[str]:
            if not opening and not closing:
                yield ""

            if opening:
                for rest in generate(opening - 1, closing + 1):
                    yield "(" + rest

            if closing:
                for rest in generate(opening, closing - 1):
                    yield ")" + rest

        return list(generate(n, 0))