-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseSubstringsBetweenEachPairOfParentheses.py
52 lines (42 loc) · 1.41 KB
/
ReverseSubstringsBetweenEachPairOfParentheses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution:
def reverseParentheses(self, s: str) -> str:
n = len(s)
open_indices = []
pair = [0] * n
for i, c in enumerate(s):
if c == "(":
open_indices.append(i)
elif c == ")":
j = open_indices.pop()
pair[i] = j
pair[j] = i
current = 0
res = ""
direction = 1
while current < n:
if s[current] == "(" or s[current] == ")":
current = pair[current]
direction *= -1
else:
res += s[current]
current += direction
return res
# first solution: stack of stacks, O(n^2)
# stacks: List[List[str]] = []
# res = ""
# for i in range(len(s)):
# if s[i] != "(" and s[i] != ")" and not stacks:
# res += s[i]
# elif s[i] == "(":
# stacks.append([])
# elif s[i] != ")": # "regular" chars
# stacks[-1].append(s[i])
# else: # is ")"
# if len(stacks) == 1:
# while stacks[-1]:
# res += stacks[-1].pop()
# else: # greater than 1
# while stacks[-1]:
# stacks[-2].append(stacks[-1].pop())
# stacks.pop()
# return res