Skip to content

Commit 1e8dcbd

Browse files
Add BF optimized solution for generating the paranthesis.
1 parent c37b1e1 commit 1e8dcbd

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +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)
54+
19. [Leet Code 22 Generate Paranthesis](https://leetcode.com/problems/generate-parentheses/) | [Solution Brute Force](level_medium/LeetCode_22_Generate_Paranthesis_1.ts) | [Solution BF Optimized](level_medium/LeetCode_22_Generate_Paranthesis_2.ts)
5555

5656

5757
## Hard
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Leverages the permutation solution as discussed in the last solution. However, the solution
3+
* also leverages some optimization where the the validity of the solution is checked along while
4+
* building the solutions, rather than the running an operation of O(n) later to complete the validation.
5+
*
6+
* Complexity: Time: O(2^n) time complexity for generating all possible pairs.
7+
* Space: O(2^n) for the entire solution space.
8+
*
9+
* @param n
10+
*/
11+
function generateParenthesis(n: number): string[] {
12+
return generateParanthesisHelper(n * 2)
13+
};
14+
15+
function generateParanthesisHelper(n: number, curr: number = 0, result: string[] = [], braces: number = 0): string[] {
16+
17+
const output: string[] = [];
18+
19+
if (curr >= n) {
20+
// Done for all the positions, validate the created string.
21+
if (braces === 0)
22+
output.push(result.join(""))
23+
24+
return output;
25+
}
26+
27+
// At the position curr, choose one of the braces and recursively move.
28+
result[curr] = "("
29+
output.push(...generateParanthesisHelper(n, curr + 1, result, braces + 1));
30+
31+
// At the position curr, choose one of the braces and recursively move.
32+
if (braces >= 1) {
33+
34+
// Only make this choice, if it won't make the string valid.
35+
result[curr] = ")"
36+
output.push(...generateParanthesisHelper(n, curr + 1, result, braces - 1));
37+
}
38+
39+
// Return the final results.
40+
return output;
41+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy