File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,7 @@ Happy to accept any contributions to this in any langugage.
50
50
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 )
51
51
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 )
52
52
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 )
53
54
54
55
55
56
## Hard
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments