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

# Minimum Remove to Make Valid Parentheses

> Tested Python solution for LeetCode 1249 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 1249, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Stack](/catalog/topics/stack). [View on LeetCode](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/description/).

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

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

## Problem

Given a string `s` of `'('` , `')'` and lowercase English characters.

Your task is to remove the minimum number of parentheses ( `'('` or `')'`, in any positions ) so that the resulting *parentheses string* is valid and return **any** valid string.

Formally, a *parentheses string* is valid if and only if:

* It is the empty string, contains only lowercase characters, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are valid strings, or
* It can be written as `(A)`, where `A` is a valid string.

### Examples

```
Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
```

```
Input: s = "a)b(c)d"
Output: "ab(c)d"
```

```
Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.
```

### Constraints

* `1 <= s.length <= 10^5`
* `s[i]` is either `'('` , `')'`, or lowercase English letter.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(s))
    # Space: O(len(s))
    def min_remove_to_make_valid(self, s: str) -> str:
        chars = list(s)
        stack: list[int] = []
        for i, char in enumerate(chars):
            if char == "(":
                stack.append(i)
            elif char == ")":
                if stack:
                    stack.pop()
                else:
                    chars[i] = ""
        for i in stack:
            chars[i] = ""
        return "".join(chars)
```

## Complexity

| Time | Space |
| - | - |
| O(len(s)) | O(len(s)) |

## Tags

[NeetCode All](/catalog/neetcode).


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