(n, combos = [])
| 62 | var generateParenthesis = (n) => bfs(n); |
| 63 | |
| 64 | const bfs = (n, combos = []) => { |
| 65 | const queue = new Queue([['', 0, 0]]); |
| 66 | |
| 67 | while (!queue.isEmpty()) { |
| 68 | /* Time O(2^N) */ |
| 69 | const [str, open, close] = queue.dequeue(); |
| 70 | |
| 71 | const isBaseCase = open === n && close === n; |
| 72 | if (isBaseCase) { |
| 73 | combos.push(str); /* Space O(N) */ |
| 74 | |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | const isOpen = open < n; |
| 79 | if (isOpen) |
| 80 | queue.enqueue([`${str}(`, open + 1, close]); /* Space O(2^N) */ |
| 81 | |
| 82 | const isClose = close < open; |
| 83 | if (isClose) |
| 84 | queue.enqueue([`${str})`, open, close + 1]); /* Space O(2^N) */ |
| 85 | } |
| 86 | |
| 87 | return combos; |
| 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * DFS |
no test coverage detected