Skip to content

Commit b47abdc

Browse files
committed
feat(leetcode): 2116 Check if a Parentheses String Can Be Valid
1 parent 2655d48 commit b47abdc

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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>&#39;(&#39;</code> and <code>&#39;)&#39;</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>&#39;0&#39;</code>s and <code>&#39;1&#39;</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>&#39;1&#39;</code>, you <strong>cannot</strong> change <code>s[i]</code>.</li>
14+
<li>But if <code>locked[i]</code> is <code>&#39;0&#39;</code>, you <strong>can</strong> change <code>s[i]</code> to either <code>&#39;(&#39;</code> or <code>&#39;)&#39;</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>&nbsp;</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 = &quot;))()))&quot;, locked = &quot;010100&quot;
24+
<strong>Output:</strong> true
25+
<strong>Explanation:</strong> locked[1] == &#39;1&#39; and locked[3] == &#39;1&#39;, so we cannot change s[1] or s[3].
26+
We change s[0] and s[4] to &#39;(&#39; 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 = &quot;()()&quot;, locked = &quot;0000&quot;
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 = &quot;)&quot;, locked = &quot;0&quot;
40+
<strong>Output:</strong> false
41+
<strong>Explanation:</strong> locked permits us to change s[0].
42+
Changing s[0] to either &#39;(&#39; or &#39;)&#39; will not make s valid.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>n == s.length == locked.length</code></li>
50+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
51+
<li><code>s[i]</code> is either <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>.</li>
52+
<li><code>locked[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</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 '('?
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"difficulty": "medium",
3+
"similarQuestions": [
4+
{
5+
"title": "Valid Parentheses",
6+
"titleSlug": "valid-parentheses",
7+
"difficulty": "Easy",
8+
"translatedTitle": null
9+
},
10+
{
11+
"title": "Generate Parentheses",
12+
"titleSlug": "generate-parentheses",
13+
"difficulty": "Medium",
14+
"translatedTitle": null
15+
},
16+
{
17+
"title": "Valid Parenthesis String",
18+
"titleSlug": "valid-parenthesis-string",
19+
"difficulty": "Medium",
20+
"translatedTitle": null
21+
},
22+
{
23+
"title": "Minimum Remove to Make Valid Parentheses",
24+
"titleSlug": "minimum-remove-to-make-valid-parentheses",
25+
"difficulty": "Medium",
26+
"translatedTitle": null
27+
},
28+
{
29+
"title": " Check if There Is a Valid Parentheses String Path",
30+
"titleSlug": "check-if-there-is-a-valid-parentheses-string-path",
31+
"difficulty": "Hard",
32+
"translatedTitle": null
33+
}
34+
]
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Author: 1chooo<hugo970217@gmail.com>
3+
* Problem: https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/
4+
* Runtime: ms (%)
5+
*/
6+
7+
const static auto _ = []() {
8+
cin.tie(nullptr)->sync_with_stdio(false);
9+
return nullptr;
10+
}();
11+
12+
class Solution {
13+
public:
14+
bool canBeValid(string s, string locked) {
15+
int n = s.size();
16+
17+
if (n % 2)
18+
return false;
19+
20+
stack<int> openIndices;
21+
stack<int> unlockedIndices;
22+
23+
for (int i = 0; i < n; i++) {
24+
if (locked[i] == '0') {
25+
unlockedIndices.push(i);
26+
} else if (s[i] == '(') {
27+
openIndices.push(i);
28+
} else if (s[i] == ')') {
29+
if (!openIndices.empty()) {
30+
openIndices.pop();
31+
} else if (!unlockedIndices.empty()) {
32+
unlockedIndices.pop();
33+
} else {
34+
return false;
35+
}
36+
}
37+
}
38+
39+
while (!openIndices.empty() &&
40+
!unlockedIndices.empty() &&
41+
openIndices.top() < unlockedIndices.top()) {
42+
openIndices.pop();
43+
unlockedIndices.pop();
44+
}
45+
46+
if (openIndices.empty() && !unlockedIndices.empty()) {
47+
return unlockedIndices.size() % 2 == 0;
48+
}
49+
50+
return openIndices.empty();
51+
}
52+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Author: 1chooo<hugo970217@gmail.com>
3+
* Problem: https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/
4+
* Runtime: ms (%)
5+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
* Author: 1chooo<hugo970217@gmail.com>
3+
* Problem: https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/
4+
* Runtime: ms (%)
5+
"""
6+

0 commit comments

Comments
 (0)