2116. 判断一个括号字符串是否有效
为保证权益,题目请参考 2116. 判断一个括号字符串是否有效(From LeetCode).
解决方案1
Python
python
# 5948. 判断一个括号字符串是否有效
# https://leetcode-cn.com/contest/biweekly-contest-68/problems/check-if-a-parentheses-string-can-be-valid/
class Solution:
def canBeValid(self, s: str, locked: str) -> bool:
high = 0
low = 0
for i, lock in enumerate(locked):
if lock == "1":
if s[i] == "(":
high += 1
low += 1
else:
high -= 1
if high < 0:
return False
low -= 1
if low < 0:
low = 0
else:
high += 1
low -= 1
if low < 0:
low = 0
if low <= 0 and high >= 0 and high % 2 == 0:
return True
else:
return False
if __name__ == "__main__":
solution = Solution()
print(solution.canBeValid("())(()(()(())()())(())((())(()())((())))))(((((((())(()))))(",
"100011110110011011010111100111011101111110000101001101001111"))
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
32
33
34
35
36
37
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
32
33
34
35
36
37