๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/76502
ํ์ด
# ํ๋ก๊ทธ๋๋จธ์ค 2๋จ๊ณ - ์๊ฐ ์ฝ๋ ์ฑ๋ฆฐ์ง ์์ฆ2 - ๊ดํธ ํ์ ํ๊ธฐ
from collections import deque
def check(s):
answer = 0
stack = []
for i in s:
if len(stack) == 0:
stack.append(i)
else:
if i == ')' and stack[-1] == '(':
stack.pop()
elif i == '}' and stack[-1] == '{':
stack.pop()
elif i == ']' and stack[-1] == '[':
stack.pop()
else:
stack.append(i)
return 1 if len(stack) == 0 else 0
def solution(s):
answer = 0
for i in range(len(s)):
if check(s): answer += 1
s = s[1:] + s[:1]
return answer
solution("[](){}")
๋น์ทํ ๋ฌธ์ ๋ก ์ฌ๋ฐ๋ฅธ ๊ดํธ๊ฐ ์๋ค. ์ฌ๋ฐ๋ฅธ ๊ดํธ๋ฅผ ํ์ธํ๊ธฐ ์ํด ์คํ์ ์ฌ์ฉํ์๋๋ฐ ์ฌ๊ธฐ์๋ ๋ํ ์คํ์ ์ฌ์ฉํ์๋ค.
์ด๋ฆฐ ๊ดํธ๊ฐ ๋์ค๋ฉด ์คํ์ ๋์ ์ถ๊ฐ ํด์ฃผ๊ณ ๋ซํ ๊ดํธ๊ฐ ๋์ค๋ฉด ์คํ์ ์๋ ์์๋ฅผ ํ์ธํด ๊ฐ์ ๊ดํธ๋ฉด ์คํ์์ ์ ๊ฑฐํด ์ค๋ค.