Skip to content

Commit c37b1e1

Browse files
Added BF Solution for Generate Paranthesis
1 parent eb9c064 commit c37b1e1

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Happy to accept any contributions to this in any langugage.
5151
16. [LeetCode Web Crawler 1242](https://leetcode.com/problems/web-crawler-multithreaded/) | [Solution 1 ](./level_medium/LeetCode_WebCrawler_MultiThreaded_1242.java) | [Solution 2](level_medium/LeetCode_WebCrawler_MultiThreaded_1242_1.java)
5252
17. [Leet Code 554 Brick Wall](https://leetcode.com/problems/brick-wall/) | [Solution 1](./level_medium/LeetCode_554_Brick_Wall_Medium.ts)
5353
18. [Leet Code 1190 Reverse Substrings Between Each Pair of Paranthesis](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [Solution 1](level_medium/LeetCode_1190_Reverse_Substrings.ts) | [Solution Optimized](level_medium/LeetCode_1190_Reverse_Substrings_1.ts)
54+
19. [Leet Code 22 Generate Paranthesis](https://leetcode.com/problems/generate-parentheses/) | [Solution Brute Force](level_medium/LeetCode_22_Generate_Paranthesis_1.ts)
5455

5556

5657
## Hard
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Problem: Leet Code 22
3+
* Solution: Uses a brute force approach of creating all the possible permutations.
4+
* Basic fundamental of permutation is making all the possible choices at each step.
5+
* As we explore all the possible choices at each step, the solution space will explode
6+
* for us leading to an exponential solution. We use recursion to explore and a base condition
7+
* to stop where all the positions for the choices have been explored.
8+
*
9+
* Complexity: Time O(2^n * n) since the entire permutation space has to be explored.
10+
* Additionally, each string also needs to be validated for correctness in O(n) time.
11+
* Space: O(2^n) assuming all the possible strings are valid.
12+
*
13+
* @param n
14+
*/
15+
function generateParenthesis(n: number): string[] {
16+
return generateParanthesisHelper(n * 2)
17+
};
18+
19+
function generateParanthesisHelper(n: number, curr: number = 0, result: string[] = []): string[] {
20+
21+
const output: string[] = [];
22+
23+
if (curr >= n) {
24+
// Done for all the positions, validate the created string.
25+
if (isValidParanthesis(result))
26+
output.push(result.join(""))
27+
28+
return output;
29+
}
30+
31+
// At the position curr, choose one of the braces and recursively move.
32+
result[curr] = "("
33+
output.push(...generateParanthesisHelper(n, curr + 1, result));
34+
35+
// At the position curr, choose one of the braces and recursively move.
36+
result[curr] = ")"
37+
output.push(...generateParanthesisHelper(n, curr + 1, result));
38+
39+
// Return the final results.
40+
return output;
41+
42+
}
43+
44+
function isValidParanthesis(result: string[]) {
45+
let braces = 0;
46+
for (let idx = 0; idx < result.length; idx++) {
47+
if (result[idx] === "(") braces++
48+
else if (result[idx] === ")") braces--;
49+
50+
if (braces < 0) return false
51+
}
52+
53+
return braces === 0;
54+
}
55+
56+
console.log(generateParenthesis(3))

0 commit comments

Comments
 (0)