728x90
반응형

프로그래머스 연습문제 올바른 괄호

올바른괄호 문제링크

조건 두면서 확인해가는 문제였습니다.

먼저 처음에 ')' 가 들어오면 False를 return 하도록 하고, 두 짝을 cnt 변수로 확인했습니다.

def solution(s):
    answer = True
    
    cnt = 0
    if s[0] == ')':
        return False
    for ch in s:
        if cnt == 0 and ch == ')':
            return False
        if ch == '(':
            cnt += 1
        if ch == ')':
            cnt -= 1
    if cnt != 0:
        return False

    return True

오늘은 이걸로 ㅎㅎ 

이만 :)

728x90
반응형

+ Recent posts