- Reference
- ์นด์นด์ค ๊ธฐ์ถ ๋ฌธ์ ๋ ๊ทธ๋ ๊ณ ์ด๋ฐ ๋ฌธ์ ๋ค์ ์น๋ค
stack
์ ์ฌ์ฉํด์ผ ํ๋ฆฌ๋ ๋ฌธ์
and
์ฐ์ฐ์ ๊ตณ์ด ์กฐ๊ฑด ๋ชจ๋๋ฅผ ๋น๊ตํ์ง ์์
False
๊ฐ ๋์ค๋ฉด ์ฆ์ ๋น๊ต๋ฅผ ์ค๋จํจ
Solution
import collections
class Solution:
def isValid(self, s: str) -> bool:
# parentheses_map = collections.defaultdict(str)
# parentheses_map['('] = ')'
# parentheses_map['{'] = '}'
# parentheses_map['['] = ']'
# i = 0
# while i < len(s) - 1:
# if parentheses_map[s[i]] == s[i + 1]:
# i += 2
# continue
#
# return False
#
# return True
# ์์ฒ๋ผํ๋ฉด "{[]}"์ ๊ฐ์ ์ผ์ด์ค์์ ๊ฑธ๋ฌ์ง -> ์ฆ ์คํ ์จ์ผ ํ๋ ๋ฌธ์
parentheses_map = collections.defaultdict(str)
parentheses_map[')'] = '('
parentheses_map['}'] = '{'
parentheses_map[']'] = '['
stack = []
for each in s:
if stack and parentheses_map[each] == stack[-1]:
stack.pop()
continue
stack.append(each)
if stack:
return False
return True
Leave a comment