-
Notifications
You must be signed in to change notification settings - Fork 0
/
#1190 reverseParentheses.py
44 lines (32 loc) · 1.18 KB
/
#1190 reverseParentheses.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
class Solution:
def reverseParentheses(self, s: str) -> str:
# Regular stack solution
stack = []
for char in s:
if char == ')':
rev = []
while len(stack) > 0:
# Reverse until the first '(' seen in the stack
c = stack.pop()
if c == '(':
# Proceed to processing more characters
break
rev.append(c)
stack.extend(rev)
else:
stack.append(char)
return ''.join(stack)
# Alternative list solution
# ans = list(s)
# pairs = []
# opens = []
# for i, char in enumerate(s):
# if char == '(':
# opens.append(i)
# elif char == ')':
# pairs.append((opens.pop(), i))
# # The order in pairs is the order that they are closed in
# for l_idx, r_idx in pairs:
# # Reverse those characters in between
# ans[l_idx + 1:r_idx] = ans[l_idx + 1:r_idx][::-1]
# return ''.join(map(lambda x: x if x not in ('(', ')') else '', ans))