Skip to content

Commit a3b37b2

Browse files
committed
Sync LeetCode submission Runtime - 63 ms (91.64%), Memory - 21.8 MB (7.78%)
1 parent d4d232c commit a3b37b2

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<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>
2+
3+
<ul>
4+
<li>It is <code>()</code>.</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 parentheses strings.</li>
6+
<li>It can be written as <code>(A)</code>, where <code>A</code> is a valid parentheses string.</li>
7+
</ul>
8+
9+
<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>
10+
11+
<ul>
12+
<li>If <code>locked[i]</code> is <code>&#39;1&#39;</code>, you <strong>cannot</strong> change <code>s[i]</code>.</li>
13+
<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>
14+
</ul>
15+
16+
<p>Return <code>true</code> <em>if you can make <code>s</code> a valid parentheses string</em>. Otherwise, return <code>false</code>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/06/eg1.png" style="width: 311px; height: 101px;" />
21+
<pre>
22+
<strong>Input:</strong> s = &quot;))()))&quot;, locked = &quot;010100&quot;
23+
<strong>Output:</strong> true
24+
<strong>Explanation:</strong> locked[1] == &#39;1&#39; and locked[3] == &#39;1&#39;, so we cannot change s[1] or s[3].
25+
We change s[0] and s[4] to &#39;(&#39; while leaving s[2] and s[5] unchanged to make s valid.</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> s = &quot;()()&quot;, locked = &quot;0000&quot;
31+
<strong>Output:</strong> true
32+
<strong>Explanation:</strong> We do not need to make any changes because s is already valid.
33+
</pre>
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> s = &quot;)&quot;, locked = &quot;0&quot;
39+
<strong>Output:</strong> false
40+
<strong>Explanation:</strong> locked permits us to change s[0].
41+
Changing s[0] to either &#39;(&#39; or &#39;)&#39; will not make s valid.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>n == s.length == locked.length</code></li>
49+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
50+
<li><code>s[i]</code> is either <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>.</li>
51+
<li><code>locked[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
52+
</ul>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Approach 1: Stack
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def canBeValid(self, s: str, locked: str) -> bool:
8+
length = len(s)
9+
10+
# If length of string is odd, return false.
11+
if length % 2 == 1:
12+
return False
13+
14+
open_brackets = []
15+
unlocked = []
16+
17+
# Iterate through the string to handle '(' and ')'
18+
for i in range(length):
19+
if locked[i] == '0':
20+
unlocked.append(i)
21+
elif s[i] == '(':
22+
open_brackets.append(i)
23+
elif s[i] == ')':
24+
if open_brackets:
25+
open_brackets.pop()
26+
elif unlocked:
27+
unlocked.pop()
28+
else:
29+
return False
30+
31+
# Match remaining open brackets and the unlocked characters
32+
while open_brackets and unlocked and open_brackets[-1] < unlocked[-1]:
33+
open_brackets.pop()
34+
unlocked.pop()
35+
36+
if open_brackets:
37+
return False
38+
39+
return True
40+

0 commit comments

Comments
 (0)