


from collections import deque
def solution(s):
pairs = {']': '[', '}': '{', ')': '('}
answer = 0
for i in range(len(s)):
new_s = deque(s)
new_s.rotate(-i)
stack = []
for j in new_s:
if j in ')]}':
if stack and (stack[-1] == pairs[j]):
stack.pop()
continue
else:
break
stack.append(j)
else:
if not stack:
answer += 1
return answer
def is_valid(s):
stack = []
for ch in s:
if not stack:
stack.append(ch)
elif stack[-1] == '(':
if ch==')': stack.pop()
else: stack.append(ch)
elif stack[-1] == '{':
if ch=='}': stack.pop()
else: stack.append(ch)
elif stack[-1] == '[':
if ch==']': stack.pop()
else: stack.append(ch)
return False if stack else True
def solution(s):
answer = 0
for i in range(len(s)):
answer += is_valid(s[i:]+s[:i])
return answer
https://www.acmicpc.net/problem/9012
https://www.acmicpc.net/problem/2504