[프로그래머스] 올바른 괄호

지수 🤓·2020년 2월 20일
0

알고리즘

목록 보기
3/15

'()'를 지워가면서 이전 lenght와 비교해서 변화가 없으면 괄호가 올바르지 않을 것이라고 생각했다. 근데 효율성 부분에서 시간초과가 났다
replace 는 문자열을 복사해서 가공하기 때문!!

def solution(s):
    answer = True
    before_length = 0
    while len(s) != before_length:
        before_length = len(s)
        s = s.replace('()', '')
    if before_length > 0:
        return False
    return True

간지나게 풀려고 했으나 정석이 좋았다

def solution(s):
    stack = []
    for i in s:
        if i == '(':
            stack.append(0)
        else:
            if len(stack) < 1:
                return False
            stack.pop()

    return len(stack) == 0

원래 return len(stack) == 0 부분을 if 문을 써서 했는데 그럴 필요가 없었다.

profile
Backend Junior Developer

0개의 댓글