|
| 1 | +<h2>921. Minimum Add to Make Parentheses Valid</h2><h3>Medium</h3><hr><div><p>A parentheses string is valid if and only if:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li>It is the empty string,</li> |
| 5 | + <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> |
| 6 | + <li>It can be written as <code>(A)</code>, where <code>A</code> is a valid string.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<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> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li>For example, if <code>s = "()))"</code>, you can insert an opening parenthesis to be <code>"(<strong>(</strong>)))"</code> or a closing parenthesis to be <code>"())<strong>)</strong>)"</code>.</li> |
| 13 | +</ul> |
| 14 | + |
| 15 | +<p>Return <em>the minimum number of moves required to make </em><code>s</code><em> valid</em>.</p> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | +<p><strong>Example 1:</strong></p> |
| 19 | + |
| 20 | +<pre><strong>Input:</strong> s = "())" |
| 21 | +<strong>Output:</strong> 1 |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong>Example 2:</strong></p> |
| 25 | + |
| 26 | +<pre><strong>Input:</strong> s = "(((" |
| 27 | +<strong>Output:</strong> 3 |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p><strong>Example 3:</strong></p> |
| 31 | + |
| 32 | +<pre><strong>Input:</strong> s = "()" |
| 33 | +<strong>Output:</strong> 0 |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong>Example 4:</strong></p> |
| 37 | + |
| 38 | +<pre><strong>Input:</strong> s = "()))((" |
| 39 | +<strong>Output:</strong> 4 |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>1 <= s.length <= 1000</code></li> |
| 47 | + <li><code>s[i]</code> is either <code>'('</code> or <code>')'</code>.</li> |
| 48 | +</ul> |
| 49 | +</div> |
0 commit comments