Skip to content

Commit 05b9a5d

Browse files
Added solution for leet code 1190.
1 parent 410707a commit 05b9a5d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Happy to accept any contributions to this in any langugage.
5050
15. [Leet Code 362 Design Hit Counter](https://leetcode.com/problems/design-hit-counter/) | [Solution 1](./level_medium/LeetCode_362_Hit_Counter_1.ts) | [Solution 2](./level_medium/LeetCode_362_Hit_Counter_Medium.ts)
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)
53+
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)
5354

5455

5556
## Hard
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Problem: Leet Code 1190. Reverse Substrings between each pair of paranthesis.
3+
*
4+
* Solution: Uses the stack approach of reversing the elements of the array. Also the
5+
* same approach as used in the problems requiring backtracking where paranthesis is
6+
* involved. The approach uses the stack as the final value which will not be reversed
7+
* in the last. This allows for storing additional chars in the array without using
8+
* additional auxilliary array.
9+
*
10+
* The elements of the internal brackets are added after reversing them back into the
11+
* stack for further processing.
12+
*
13+
* Complexity: Time O(n), Space O(n)
14+
*
15+
* @param s
16+
*/
17+
function reverseParentheses(s: string): string {
18+
const stack: string[] = []
19+
for (let idx = 0; idx < s.length; idx++) {
20+
const char: string = s[idx];
21+
if (char === ')') {
22+
// Start pulling from the stack and adding to result.
23+
let elem = stack.pop();
24+
const reversed: string[] = [];
25+
while(elem !== '(') {
26+
if (elem !== undefined) reversed.push(elem);
27+
elem = stack.pop();
28+
}
29+
stack.push(...reversed);
30+
} else {
31+
stack.push(char);
32+
}
33+
}
34+
35+
return stack.join("");
36+
};

0 commit comments

Comments
 (0)