Skip to content

Commit 95678ff

Browse files
authored
Create 1190_Reverse_Substrings_Between_Each_Pair_of_Parentheses.md
1 parent 2b7af01 commit 95678ff

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## 1190. Reverse Substrings Between Each Pair of Parentheses
2+
3+
```
4+
You are given a string s that consists of lower case English letters and brackets.
5+
6+
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
7+
8+
Your result should not contain any brackets.
9+
10+
11+
12+
Example 1:
13+
14+
Input: s = "(abcd)"
15+
Output: "dcba"
16+
Example 2:
17+
18+
Input: s = "(u(love)i)"
19+
Output: "iloveu"
20+
Explanation: The substring "love" is reversed first, then the whole string is reversed.
21+
Example 3:
22+
23+
Input: s = "(ed(et(oc))el)"
24+
Output: "leetcode"
25+
Explanation: First, we reverse the substring "oc", then "etco", and finally, the whole string.
26+
Example 4:
27+
28+
Input: s = "a(bcdefghijkl(mno)p)q"
29+
Output: "apmnolkjihgfedcbq"
30+
31+
32+
Constraints:
33+
34+
0 <= s.length <= 2000
35+
s only contains lower case English characters and parentheses.
36+
It's guaranteed that all parentheses are balanced.
37+
```
38+
39+
40+
41+
```python
42+
class Solution:
43+
def reverseParentheses(self, s: str) -> str:
44+
st = []
45+
for c in s:
46+
if c == '(':
47+
st.append(c)
48+
49+
elif c == ')':
50+
res = []
51+
while st and st[-1] != '(':
52+
res.append(st.pop())
53+
st.pop()
54+
st += res
55+
else:
56+
st.append(c)
57+
58+
return "".join(st)
59+
```
60+
61+
62+
```
63+
Runtime: 36 ms, faster than 30.44% of Python3 online submissions for Reverse Substrings Between Each Pair of Parentheses.
64+
Memory Usage: 14.2 MB, less than 78.52% of Python3 online submissions for Reverse Substrings Between Each Pair of Parentheses.
65+
```

0 commit comments

Comments
 (0)