valid parenteses brackets leetcode
#605
- Author
- socdev
- Created
- Dec. 4, 2022, 8:43 p.m.
- Expires
- Never
- Size
- 590 bytes
- Hits
- 56
- Syntax
- Python
- Private
- ✗ No
#https://leetcode.com/problems/valid-parentheses/
class Solution:
def isValid(self, s: str) -> bool:
stack = []
d = {
")":"(",
"}":"{",
"]":"[",
}
for c in s:
if c in d:
if stack and stack[-1] == d[c]:
stack.pop()
else:
return False
else:
stack.append(c)
if stack:
return False
else:
return True