Check if a Parentheses String Can Be Valid LeetCode solution in Python
1# Runtime: 460 ms (Top 26.24%) | Memory: 15.4 MB (Top 86.61%)2class Solution:3def canBeValid(self, s: str, locked: str) -> bool:4def validate(s: str, locked: str, op: str) -> bool:5bal, wild = 0, 06for i in range(len(s)):7if locked[i] == "1":8bal += 1 if s[i] == op else -19else:10wild += 111if wild + bal < 0:12return False13return bal <= wild1415return (16len(s) % 2 == 017and validate(s, locked, "(")18and validate(s[::-1], locked[::-1], ")")19)