We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 04b7acc commit 134207bCopy full SHA for 134207b
typescript/22-Generate-Parentheses.ts
@@ -0,0 +1,27 @@
1
+function generateParenthesis(n: number): string[] {
2
+ const stack: string[] = [];
3
+ const res: string[] = [];
4
+
5
+ function backtrack(openN: number, closedN: number) {
6
+ if (openN === n && closedN === n) {
7
+ res.push(stack.join(""));
8
+ return;
9
+ }
10
11
+ if (openN < n) {
12
+ stack.push("(");
13
+ backtrack(openN + 1, closedN);
14
+ stack.pop();
15
16
17
+ if (closedN < openN) {
18
+ stack.push(")");
19
+ backtrack(openN, closedN + 1);
20
21
22
23
24
+ backtrack(0, 0);
25
26
+ return res;
27
+}
0 commit comments