> For the complete documentation index, see [llms.txt](https://txfs19260817.gitbook.io/leetcode-go-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://txfs19260817.gitbook.io/leetcode-go-notes/solutions/1190.-reverse-substrings-between-each-pair-of-parentheses.md).

# 1190. Reverse Substrings Between Each Pair of Parentheses

## LeetCode [1190. Reverse Substrings Between Each Pair of Parentheses](https://github.com/txfs19260817/LeetCode-Go/blob/master/solutions/title/README.md)

### Description

You are 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 brackets.

**Example 1:**

```
Input: s = "(abcd)"
Output: "dcba"
```

**Example 2:**

```
Input: s = "(u(love)i)"
Output: "iloveu"
Explanation: The substring "love" is reversed first, then the whole string is reversed.
```

**Example 3:**

```
Input: s = "(ed(et(oc))el)"
Output: "leetcode"
Explanation: First, we reverse the substring "oc", then "etco", and finally, the whole string.
```

**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.

### Tags

Stack

### Solution

![](/files/B7atTD2Dc4Vf8ASBO36V)

The rule is that, if we meet any parenthesis, we move the pointer to the index of the corresponded parenthesis, and iterate over the input string in reverse order.

Therefore, our first step is to build the index mapping for each pair of parentheses with stack. Specifically, we build an array `pair` to record the index should jump to if we meet a parenthesis. Using stack, if we find a pair of parentheses whose left and right indices are `i` and `j`, respectively, we do `pair[i], pair[j] = j, i`.

Then, we start to perform the rule. Traverse the input string and append any non-parenthesis characters into a byte array. If we meet a parenthesis, we move `i` to `pair[i]` and reverse the direction of moving.

Finally, return the byte array in `string` type.

### Complexity

* Time complexity: $$O(n)$$
* Space complexity: $$O(n)$$

### Code

```go
func reverseParentheses(s string) string {
	pair, stack, ans := make([]int, len(s)), make([]int, 0, len(s)), make([]byte, 0, len(s))
	for i, c := range s {
		switch c {
		case '(':
			stack = append(stack, i)
		case ')':
			j := stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			pair[i], pair[j] = j, i
		}
	}
	for i, dir := 0, 1; i < len(s); i += dir {
		if s[i] == '(' || s[i] == ')' {
			i = pair[i]
			dir *= -1
			continue
		}
		ans = append(ans, s[i])
	}
	return string(ans)
}
```

## Reference

1. [反转每对括号间的子串](https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses/solution/fan-zhuan-mei-dui-gua-hao-jian-de-zi-chu-gwpv/)
