-
Notifications
You must be signed in to change notification settings - Fork 161
/
reverse-substrings-between-each-pair-of-parentheses.md
45 lines (33 loc) · 1.4 KB
/
reverse-substrings-between-each-pair-of-parentheses.md
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
<p>You are given a string <code>s</code> that consists of lower case English letters and brackets. </p>
<p>Reverse the strings in each pair of matching parentheses, starting from the innermost one.</p>
<p>Your result should <strong>not</strong> contain any brackets.</p>
<p> </p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "(abcd)"
<strong>Output:</strong> "dcba"
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "(u(love)i)"
<strong>Output:</strong> "iloveu"
<strong>Explanation:</strong> The substring "love" is reversed first, then the whole string is reversed.
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "(ed(et(oc))el)"
<strong>Output:</strong> "leetcode"
<strong>Explanation:</strong> First, we reverse the substring "oc", then "etco", and finally, the whole string.
</pre>
<p><strong>Example 4:</strong></p>
<pre>
<strong>Input:</strong> s = "a(bcdefghijkl(mno)p)q"
<strong>Output:</strong> "apmnolkjihgfedcbq"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= s.length <= 2000</code></li>
<li><code>s</code> only contains lower case English characters and parentheses.</li>
<li>It's guaranteed that all parentheses are balanced.</li>
</ul>