Skip to content

Commit 269cfc8

Browse files
Add Generate Parentheses algorithm in TypeScript (#166)
* Add Generate Parentheses algorithm in TypeScript * Fix typo --------- Co-authored-by: IcarusTheFly <IcarusTheFly@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
1 parent 6fc7f3f commit 269cfc8

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

backtracking/generateParentheses.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Given a number n pairs of parentheses, generate all combinations of valid parentheses
3+
* @param {number} n: Number of given parentheses
4+
* @return {string[]} result: Array that contains all valid parentheses
5+
* @see https://leetcode.com/problems/generate-parentheses/
6+
*/
7+
8+
const generateParentheses = (n: number): string[] => {
9+
const result: string[] = [];
10+
11+
const solve = (chars: string, openParentheses: number, closedParentheses: number) => {
12+
if (openParentheses === n && closedParentheses === n) {
13+
result.push(chars);
14+
return;
15+
}
16+
17+
if (openParentheses <= n) {
18+
solve(chars + "(", openParentheses + 1, closedParentheses);
19+
}
20+
21+
if (closedParentheses < openParentheses) {
22+
solve(chars + ")", openParentheses, closedParentheses + 1);
23+
}
24+
};
25+
26+
solve("", 0, 0);
27+
28+
return result;
29+
};
30+
31+
export { generateParentheses };
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { generateParentheses } from "../generateParentheses";
2+
3+
const cases: [number, string[]][] = [
4+
[0, [""]],
5+
[1, ["()"]],
6+
[2, ["(())", "()()"]],
7+
[3, ["((()))", "(()())", "(())()", "()(())", "()()()"]],
8+
[
9+
4,
10+
[
11+
"(((())))",
12+
"((()()))",
13+
"((())())",
14+
"((()))()",
15+
"(()(()))",
16+
"(()()())",
17+
"(()())()",
18+
"(())(())",
19+
"(())()()",
20+
"()((()))",
21+
"()(()())",
22+
"()(())()",
23+
"()()(())",
24+
"()()()()",
25+
],
26+
],
27+
[
28+
5,
29+
[
30+
"((((()))))",
31+
"(((()())))",
32+
"(((())()))",
33+
"(((()))())",
34+
"(((())))()",
35+
"((()(())))",
36+
"((()()()))",
37+
"((()())())",
38+
"((()()))()",
39+
"((())(()))",
40+
"((())()())",
41+
"((())())()",
42+
"((()))(())",
43+
"((()))()()",
44+
"(()((())))",
45+
"(()(()()))",
46+
"(()(())())",
47+
"(()(()))()",
48+
"(()()(()))",
49+
"(()()()())",
50+
"(()()())()",
51+
"(()())(())",
52+
"(()())()()",
53+
"(())((()))",
54+
"(())(()())",
55+
"(())(())()",
56+
"(())()(())",
57+
"(())()()()",
58+
"()(((())))",
59+
"()((()()))",
60+
"()((())())",
61+
"()((()))()",
62+
"()(()(()))",
63+
"()(()()())",
64+
"()(()())()",
65+
"()(())(())",
66+
"()(())()()",
67+
"()()((()))",
68+
"()()(()())",
69+
"()()(())()",
70+
"()()()(())",
71+
"()()()()()",
72+
],
73+
],
74+
];
75+
76+
describe("Generate Parentheses", () => {
77+
test.each(cases)(
78+
"generate all valid parentheses of input %n",
79+
(n: number, expected: string[]) => {
80+
expect(generateParentheses(n)).toStrictEqual(expected);
81+
}
82+
);
83+
});

0 commit comments

Comments
 (0)