|
| 1 | +## [2116. Check if a Parentheses String Can Be Valid](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) |
| 2 | +<p>A parentheses string is a <strong>non-empty</strong> string consisting only of <code>'('</code> and <code>')'</code>. It is valid if <strong>any</strong> of the following conditions is <strong>true</strong>:</p> |
| 3 | + |
| 4 | +<ul> |
| 5 | + <li>It is <code>()</code>.</li> |
| 6 | + <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 parentheses strings.</li> |
| 7 | + <li>It can be written as <code>(A)</code>, where <code>A</code> is a valid parentheses string.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>You are given a parentheses string <code>s</code> and a string <code>locked</code>, both of length <code>n</code>. <code>locked</code> is a binary string consisting only of <code>'0'</code>s and <code>'1'</code>s. For <strong>each</strong> index <code>i</code> of <code>locked</code>,</p> |
| 11 | + |
| 12 | +<ul> |
| 13 | + <li>If <code>locked[i]</code> is <code>'1'</code>, you <strong>cannot</strong> change <code>s[i]</code>.</li> |
| 14 | + <li>But if <code>locked[i]</code> is <code>'0'</code>, you <strong>can</strong> change <code>s[i]</code> to either <code>'('</code> or <code>')'</code>.</li> |
| 15 | +</ul> |
| 16 | + |
| 17 | +<p>Return <code>true</code> <em>if you can make <code>s</code> a valid parentheses string</em>. Otherwise, return <code>false</code>.</p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/11/06/eg1.png" style="width: 311px; height: 101px;" /> |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> s = "))()))", locked = "010100" |
| 24 | +<strong>Output:</strong> true |
| 25 | +<strong>Explanation:</strong> locked[1] == '1' and locked[3] == '1', so we cannot change s[1] or s[3]. |
| 26 | +We change s[0] and s[4] to '(' while leaving s[2] and s[5] unchanged to make s valid.</pre> |
| 27 | + |
| 28 | +<p><strong class="example">Example 2:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>Input:</strong> s = "()()", locked = "0000" |
| 32 | +<strong>Output:</strong> true |
| 33 | +<strong>Explanation:</strong> We do not need to make any changes because s is already valid. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong class="example">Example 3:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<strong>Input:</strong> s = ")", locked = "0" |
| 40 | +<strong>Output:</strong> false |
| 41 | +<strong>Explanation:</strong> locked permits us to change s[0]. |
| 42 | +Changing s[0] to either '(' or ')' will not make s valid. |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>n == s.length == locked.length</code></li> |
| 50 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 51 | + <li><code>s[i]</code> is either <code>'('</code> or <code>')'</code>.</li> |
| 52 | + <li><code>locked[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li> |
| 53 | +</ul> |
| 54 | + |
| 55 | + |
| 56 | +## Hints |
| 57 | +1. Can an odd length string ever be valid? |
| 58 | +2. From left to right, if a locked ')' is encountered, it must be balanced with either a locked '(' or an unlocked index on its left. If neither exist, what conclusion can be drawn? If both exist, which one is more preferable to use? |
| 59 | +3. After the above, we may have locked indices of '(' and additional unlocked indices. How can you balance out the locked '(' now? What if you cannot balance any locked '('? |
0 commit comments