123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import pytest
- def isValid(s: str):
- lens = len(s)
- if lens % 2 == 1:
- return False
- dic = {
- "(": ")",
- "[": "]",
- "{": "}"
- }
-
- list = []
- for i in s:
- if i in dic.keys():
-
- list.append(i)
- else:
-
- if len(list) == 0 or i != dic.get(list[-1]):
- list.append(i)
- else:
- list.pop()
-
- if len(list) == 0:
- return True
- else:
- return False
- @pytest.mark.parametrize(
- "s, expect",
- [
- ("()[]", True),
- ("(({[])}", False),
- ("(]", False),
- ("([{}])", True),
- ("21[o{00}j]", False)
- ]
- )
- def test_cases(s, expect):
- assert isValid(s) == expect
|