> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Reverse Substrings Between Each Pair of

> Tested Python solution for LeetCode 1190 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1190, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Stack](/catalog/topics/stack), Bracket Sequences. [View on LeetCode](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1190   # by problem number
lcpy gen -s reverse_substrings_between_each_pair_of_parentheses   # by problem name
```

## Problem

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.

### Examples

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

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

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

### Constraints

* `1 <= s.length <= 2000`
* `s` only contains lower case English characters and parentheses.
* It is guaranteed that all parentheses are balanced.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/reverse_substrings_between_each_pair_of_parentheses/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/reverse_substrings_between_each_pair_of_parentheses/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2) worst case, O(n) average
    # Space: O(n)
    def reverse_parentheses(self, s: str) -> str:
        stack: list[str] = []
        for char in s:
            if char == ")":
                segment: list[str] = []
                while stack and stack[-1] != "(":
                    segment.append(stack.pop())
                stack.pop()
                stack.extend(segment)
            else:
                stack.append(char)
        return "".join(stack)
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) worst case, O(n) average | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.