최대 1 분 소요

문제 링크

내 풀이

def solution(s):
    g_list = []
    for word in s:
        if word == '(':
            g_list.append(word)
        else:
            if g_list:
                if g_list[-1] == '(':
                    g_list.pop()
            else:
                return False
    if g_list:
        return False
    
    return True

다른 풀이

def is_pair(s):
    st = list()
    for c in s:
        if c == '(':
            st.append(c)

        if c == ')':
            try:
                st.pop()
            except IndexError:
                return False

    return len(st) == 0

풀이 해석

  • try except를 이용해 list가 비어있을 경우 발생하는 오류를 사전에 방지했다

배울 점

  • try except를 이용해 오류 방지

댓글남기기