동캄의 코딩도장

프로그래머스 level2 [괄호 회전하기] 파이썬 본문

코테/프로그래머스

프로그래머스 level2 [괄호 회전하기] 파이썬

동 캄 2021. 12. 6. 22:02

 

https://programmers.co.kr/learn/courses/30/lessons/76502

 

코딩테스트 연습 - 괄호 회전하기

 

programmers.co.kr

#프로그래머스 괄호 회전하기 다른사람풀이
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

하나하나씩 다 회전하는걸 시도하지 못했다... 위의 풀이를 참고해서 다시 풀이를 수정하였다.

#프로그래머스 괄호 회전하기
def solution(s):
    answer = 0
    s=list(map(str,s))
    if len(s)%2==1:
        return 0
    else:
        for _ in range(len(s)):
            lst=[]
            for i in range(len(s)):
                lst.append(s[i])
                if len(lst)>1:
                    if lst[-2]=='(' and lst[-1]==')':
                        lst.pop()
                        lst.pop()
                    elif lst[-2]=='{' and lst[-1]=='}':
                        lst.pop()
                        lst.pop()
                    elif lst[-2]=='[' and lst[-1]==']':
                        lst.pop()
                        lst.pop()
            if not lst:
                answer+=1
            s.append(s.pop(0))
        return answer