Files
freeCodeCamp/curriculum/challenges/english/blocks/daily-coding-challenges-python/69f90c1329a94b37e2a2086d.md
T

1.7 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69f90c1329a94b37e2a2086d Challenge 294: Parentheses Combinations 29 challenge-294

--description--

Given an integer, n, return the number of valid combinations of n pairs of parentheses.

  • A valid combination is a string where every opening parentheses has a corresponding closing parentheses, and no closing parentheses appears before its matching opening parentheses.

For example, given 2, there are 2 valid combinations:

(())
()()

--hints--

get_combinations(2) should return 2.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_combinations(2), 2)`)
}})

get_combinations(3) should return 5.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_combinations(3), 5)`)
}})

get_combinations(5) should return 42.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_combinations(5), 42)`)
}})

get_combinations(8) should return 1430.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_combinations(8), 1430)`)
}})

get_combinations(13) should return 742900.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_combinations(13), 742900)`)
}})

--seed--

--seed-contents--

def get_combinations(n):

    return n

--solutions--

def get_combinations(n):
    count = 0

    def generate(op, cl):
        nonlocal count
        if op == 0 and cl == 0:
            count += 1
            return
        if op > 0:
            generate(op - 1, cl)
        if cl > op:
            generate(op, cl - 1)

    generate(n, n)
    return count