MCPcopy Index your code
hub / github.com/neetcode-gh/leetcode / backtrack

Method backtrack

java/0022-generate-parentheses.java:23–41  ·  view source on GitHub ↗
(int openN, int closedN, int n)

Source from the content-addressed store, hash-verified

21 }
22
23 private void backtrack(int openN, int closedN, int n) {
24 if (openN == closedN && closedN == n) {
25 StringBuilder sb = new StringBuilder();
26 for (Character c: stack) {
27 sb.append(c);
28 }
29 res.add(sb.toString());
30 }
31 if (openN < n) {
32 stack.push('(');
33 backtrack(openN + 1, closedN, n);
34 stack.pop();
35 }
36 if (closedN < openN) {
37 stack.push(')');
38 backtrack(openN, closedN + 1, n);
39 stack.pop();
40 }
41 }
42}

Callers 1

generateParenthesisMethod · 0.95

Calls 4

addMethod · 0.45
toStringMethod · 0.45
pushMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected