프로그래머스 연습문제 -올바른괄호(level2)

j_wisdom_h·2022년 11월 16일
0

CodingTest

목록 보기
9/58
post-thumbnail

프로그래머스 연습문제 -올바른괄호(level2)

문제설명


제한사항 & 입출력 예


My Solution

def solution(s):
    answer = True
    open = []   

    for i in s:
        if i == '(':  
            open.append(i) 
        else: 
            if len(open)==0: 
                answer = False
                break
            else:
                open.pop() 
                
    if len(open) != 0: 
          answer = False

    return answer

다른 풀이 (try- catch)

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
profile
뚜잇뚜잇 FE개발자

0개의 댓글