-
Notifications
You must be signed in to change notification settings - Fork 643
Expand file tree
/
Copy path1190.py
More file actions
53 lines (41 loc) · 1.15 KB
/
1190.py
File metadata and controls
53 lines (41 loc) · 1.15 KB
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
53
'''
Given a string s that consists of lower case English letters and brackets.
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should not contain any bracket.
Example 1:
Input: s = "(abcd)"
Output: "dcba"
Example 2:
Input: s = "(u(love)i)"
Output: "iloveu"
Example 3:
Input: s = "(ed(et(oc))el)"
Output: "leetcode"
Example 4:
Input: s = "a(bcdefghijkl(mno)p)q"
Output: "apmnolkjihgfedcbq"
Constraints:
0 <= s.length <= 2000
s only contains lower case English characters and parentheses.
It's guaranteed that all parentheses are balanced.
'''
class Solution(object):
def reverseParentheses(self, s):
"""
:type s: str
:rtype: str
"""
if not s:
return ''
stack = []
for char in s:
if char == ')':
combine_str = ''
while stack and stack[-1] != '(':
elem = stack.pop()[::-1]
combine_str += elem
stack.pop()
stack.append(combine_str)
else:
stack.append(char)
return "".join(stack)