题解 | #合法的括号字符串#
合法的括号字符串
https://www.nowcoder.com/practice/eceb50e041ec40bd93240b8b3b62d221
运用列表特性:
class Solution: def isValidString(self , s: str) -> bool: res = [] for i in s: if i == '*' or i == '(': res.append(i) else: if not res: return False n = -1 while n>-len(res) and res[n]!='(': # 从后往前,如果找到了左括号,就pop掉,没有找到的话就pop掉第一个元素 n -= 1 res.pop(n) while res.count('('): if res.pop() == '*': res.remove('(') else: return False return True