Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-python/69f90c1329a94b37e2a2086d.md
T
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

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