Coding Test Practice/Python

[프로그래머스 코딩테스트 연습 - 스택/큐 Lv. 2] 올바른 괄호

y2r1m 2023. 4. 5. 18:46

https://school.programmers.co.kr/learn/courses/30/lessons/12909

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

def solution(s):
    answer = False
    ans = []
    for i in s:
        if i == '(':
            ans.append(0)
        elif len(ans) != 0:
            ans.pop()
        else: # ans에 아무것도 없는데 ")"가 나오는 경우. 즉 "("가 나오지 않고 ")"가 나오는 경우
            return False
    
    if len(ans) == 0:
        answer = True
        
    return answer