Programmers 올바른 괄호

Solution

def solution(s):
    stack = []
    for p in s:
        if p == '(':
            stack.append(p)
        else:
            if stack:
                if stack[-1] == '(':
                    stack.pop(-1)
            else:
                stack.append(p)
    return len(stack) == 0
  • 다른 사람 풀이
def is_pair(s):
    x = 0
    for w in s:
        if x < 0:
            break
        x = x+1 if w=="(" else x-1 if w==")" else x
    return x==0
  • 풀어쓴 코드
def solution(s):
    x = 0
    for w in s:
        if x < 0:
            break
        if w == "(":
            x = x + 1
        elif w == ")":
            x = x - 1
        else:
            continue
    return x == 0