> ## 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 Add to Make Parentheses Valid

> Tested Python solution for LeetCode 921 with 15 pytest cases. Generate a practice environment with lcpy.

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

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

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

## Problem

\<p>A parentheses string is valid if and only if:\</p>

\<ul>
\<li>It is the empty string,\</li>
\<li>It can be written as \<code>AB\</code> (\<code>A\</code> concatenated with \<code>B\</code>), where \<code>A\</code> and \<code>B\</code> are valid strings, or\</li>
\<li>It can be written as \<code>(A)\</code>, where \<code>A\</code> is a valid string.\</li>
\</ul>

\<p>You are given a parentheses string \<code>s\</code>. In one move, you can insert a parenthesis at any position of the string.\</p>

\<ul>
\<li>For example, if \<code>s = "()))"\</code>, you can insert an opening parenthesis to be \<code>"(()))"\</code> or a closing parenthesis to be \<code>"())))"\</code>.\</li>
\</ul>

\<p>Return \<em>the minimum number of moves required to make \</em>\<code>s\</code>\<em> valid\</em>.\</p>

### Examples

```
Input: s = "())"
Output: 1
```

```
Input: s = "((("
Output: 3
```

### Constraints

* 1 \<= s.length \<= 1000
* s\[i] is either '(' or ')'.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(?)
    # Space: O(?)
    def min_add_to_make_valid(self, s: str) -> int:
        open_needed = 0
        insertions = 0
        for char in s:
            if char == "(":
                open_needed += 1
            elif open_needed > 0:
                open_needed -= 1
            else:
                insertions += 1
        return insertions + open_needed
```

## Complexity

| Time | Space |
| - | - |
| O(?) | O(?) |

## Tags

[NeetCode All](/catalog/neetcode).


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