-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_2116.py
More file actions
31 lines (25 loc) · 804 Bytes
/
Copy pathtask_2116.py
File metadata and controls
31 lines (25 loc) · 804 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
def canBeValid(self, s: str, locked: str) -> bool:
n = len(s)
if n % 2:
return False
bal = 0
for i in range(n):
if s[i] == '(' or locked[i] == '0':
bal += 1
else:
bal -= 1
if bal < 0:
return False
bal = 0
for i in range(n-1, -1, -1):
if s[i] == ')' or locked[i] == '0':
bal += 1
else:
bal -= 1
if bal < 0:
return False
return True
print(Solution().canBeValid(s = "))()))", locked = "010100"))
print(Solution().canBeValid(s = "()()", locked = "0000"))
print(Solution().canBeValid(s = ")", locked = "0"))