def solution(s):
answer = 0
length=len(s)
s=s+s
for i in range(length):
k=s[i:i+length]
arr=[]
for j in k:
if j =="[" or j=="(" or j=="{":
arr.append(j)
elif len(arr)==0:
arr.append(0)
break
elif j=="]" and (arr[-1]=="["):
arr.pop()
elif j==")" and (arr[-1]=="("):
arr.pop()
elif j=="}" and (arr[-1]=="{"):
arr.pop()
else:
arr.append(j)
break
if len(arr)==0:
answer+=1
return answer
for i in s:
if i =="(" or i=="[":
arr.append(i)
elif i==")":
if len(arr)>=1 and arr[-1]=="(":
arr.pop()
else:
res='no'
break
elif i=="]":
if len(arr)>=1 and arr[-1]=="[":
arr.pop()
else:
res='no'
break
if len(arr)!=0 or res=='no':
print('no')
else:
print('yes')
def bracket(s):
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 bracket(s): answer +=1
s = s[1:] + s[:1]
return answer
from collections import deque
def check(s):
while True:
if "()" in s: s = s.replace("()","")
elif "{}" in s: s = s.replace("{}","")
elif "[]" in s: s = s.replace("[]","")
else: return False if s else True
def solution(s):
count = 0
queue = deque(s)
for i in range(len(s)):
if check(''.join(queue)): count+=1
queue.rotate(-1)
return count
def solution(s):
answer = 0
temp = list(s)
for _ in range(len(s)):
st = []
for i in range(len(temp)):
if len(st) > 0:
if st[-1] == '[' and temp[i] == ']':
st.pop()
elif st[-1] == '(' and temp[i] == ')':
st.pop()
elif st[-1] == '{' and temp[i] == '}':
st.pop()
else:
st.append(temp[i])
else:
st.append(temp[i])
if len(st) == 0:
answer += 1
temp.append(temp.pop(0))
return answer
pop()과 remove() 헷갈리지말자.
올바른 괄호가 되려면 arr의 마지막 요소가 입력받은 요소와 대칭된다는 점 유의하기.