mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
97 lines
1.7 KiB
Markdown
97 lines
1.7 KiB
Markdown
---
|
|
id: 69f90c1329a94b37e2a2086d
|
|
title: "Challenge 294: Parentheses Combinations"
|
|
challengeType: 29
|
|
dashedName: 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:
|
|
|
|
```json
|
|
(())
|
|
()()
|
|
```
|
|
|
|
# --hints--
|
|
|
|
`get_combinations(2)` should return `2`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_combinations(2), 2)`)
|
|
}})
|
|
```
|
|
|
|
`get_combinations(3)` should return `5`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_combinations(3), 5)`)
|
|
}})
|
|
```
|
|
|
|
`get_combinations(5)` should return `42`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_combinations(5), 42)`)
|
|
}})
|
|
```
|
|
|
|
`get_combinations(8)` should return `1430`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_combinations(8), 1430)`)
|
|
}})
|
|
```
|
|
|
|
`get_combinations(13)` should return `742900`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_combinations(13), 742900)`)
|
|
}})
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```py
|
|
def get_combinations(n):
|
|
|
|
return n
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
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
|
|
```
|