Skip to content

Commit a372b2e

Browse files
committed
Jan 12
1 parent 4bcc81a commit a372b2e

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution:
2+
def canBeValid(self, s: str, locked: str) -> bool:
3+
if len(s) % 2 != 0:
4+
return False
5+
6+
open_brackets, unlocked = [], []
7+
for i, b in enumerate(s):
8+
if locked[i] == '0':
9+
unlocked.append(i)
10+
elif b == '(':
11+
open_brackets.append(i)
12+
else:
13+
if open_brackets:
14+
open_brackets.pop()
15+
elif unlocked:
16+
unlocked.pop()
17+
else:
18+
return False
19+
20+
while open_brackets and unlocked:
21+
if open_brackets[-1] < unlocked[-1]:
22+
open_brackets.pop()
23+
unlocked.pop()
24+
else:
25+
break
26+
return not open_brackets
27+
28+
29+
def main():
30+
s = '))()))'
31+
locked = '010100'
32+
assert Solution().canBeValid(s, locked) is True
33+
34+
s = '()()'
35+
locked = '0000'
36+
assert Solution().canBeValid(s, locked) is True
37+
38+
s = ')'
39+
locked = '0'
40+
assert Solution().canBeValid(s, locked) is False
41+
42+
43+
if __name__ == '__main__':
44+
main()

2025-01-January-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
| January 9 | [2185. Counting Words With a Given Prefix](https://leetcode.com/problems/counting-words-with-a-given-prefix/) | Easy | Solved |
1515
| January 10 | [916. Word Subsets](https://leetcode.com/problems/word-subsets/) | Medium | Solved |
1616
| January 11 | [1400. Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/) | Medium | Solved |
17-
| January 12 | []() | | |
17+
| January 12 | [2116. Check if a Parentheses String Can Be Valid](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | Medium | Unsolved |
1818
| January 13 | [3223. Minimum Length of String After Operations](https://leetcode.com/problems/minimum-length-of-string-after-operations/) | Medium | Solved |
1919
| January 14 | []() | | |
2020
| January 15 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 4 | 4 | 0 |
43-
| Medium | 8 | 8 | 0 |
43+
| Medium | 9 | 8 | 1 |
4444
| Hard | 0 | 0 | 0 |

0 commit comments

Comments
 (0)