Skip to content

Commit eb9c064

Browse files
Added linear time solution for Leet Code 1190.
1 parent 05b9a5d commit eb9c064

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +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)
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) | [Solution Optimized](level_medium/LeetCode_1190_Reverse_Substrings_1.ts)
5454

5555

5656
## Hard

level_medium/LeetCode_1190_Reverse_Substrings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* The elements of the internal brackets are added after reversing them back into the
1111
* stack for further processing.
1212
*
13-
* Complexity: Time O(n), Space O(n)
13+
* Complexity: Time O(n^2) in the worst case as the loop for stack might be happening almost n/2 times.
14+
* Space O(n)
1415
*
1516
* @param s
1617
*/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Problem: Leet Code 1190.
3+
*
4+
* Solution: Solution uses an intuitive approach which is a linear time approach of solving the
5+
* problem. Given the problem, the positions of the characters within braces are reversed
6+
* a number of times. The inner positions(braces) are reversed whenever we get another
7+
* braces at the end.
8+
*
9+
* If we start producing the results from the outer boundaries in the reverse order, which is
10+
* the right to left direction, we can switch the direction of listing (left to right), upon
11+
* encountering an internal braces. This also means that the listing position should also
12+
* change from left boundary to right boundary, as opposed to right boundary to left boundary.
13+
*
14+
* The process continues changing the direction of listing and the boundary start and end
15+
* position.
16+
*
17+
* Complexity: Time O(n) and Space O(n).
18+
*
19+
* @param s
20+
*/
21+
function reverseParentheses(s: string): string {
22+
23+
// Building a pair wise list of all the braces, so that the elements within can be reversed.
24+
const pairs: number[] = Array(s.length);
25+
const stack: number[] = []
26+
for (let idx = 0; idx < s.length; idx++) {
27+
if (s[idx] === '(') {
28+
stack.push(idx);
29+
} else if (s[idx] === ')') {
30+
// Saving pairs of opposing positions which mark the boundaries for reversal.
31+
const openingPos = stack.pop();
32+
if (openingPos !== undefined) {
33+
pairs[idx] = openingPos;
34+
pairs[openingPos] = idx;
35+
}
36+
}
37+
}
38+
39+
40+
// Use the boundaries for reversal traversing in either (left->right) or (right->left)
41+
const result: string[] = []
42+
for (let pos = 0, direction = 1; pos < s.length; pos += direction) {
43+
// Push the element in the final result stack moving either to left or right.
44+
// If moving from right to left, we swtich the position to the right and direction -1
45+
// If moving from left to right, we switch position again to the left and direction to 1
46+
const elem = s[pos];
47+
if (elem === '(' || elem === ')') {
48+
// Need to change the position to reverse and direction to reverse.
49+
pos = pairs[pos];
50+
direction *= -1;
51+
} else {
52+
result.push(s[pos]);
53+
}
54+
}
55+
56+
return result.join("");
57+
};

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