[파이썬/python] 프로그래머스 : 짝지어 제거하기 (Lv.2) (스택)

https://school.programmers.co.kr/learn/courses/30/lessons/12973?language=python3 

 

프로그래머스

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

programmers.co.kr

 

def solution(s):
    stack = []
    for i in range(len(s)):
        stack.append(s[i])
        if len(stack) > 1 and stack[-1] == stack[-2] :
            stack.pop()
            stack.pop()
    if not stack:
        return 1
    else:
        return 0

def solution(s):
    stack = []
    for i in s:
        if not stack:
            stack.append(i)
        else:
            if stack[-1] == i:
                stack.pop()
            else:
                stack.append(i)
    if not stack:
        return 1
    else:
        return 0

stack이 비어 있다면 stack에 i 추가,

비어 있지 않아면 stack의 마지막 인덱스와 i가 같다면 pop,

그렇지 않다면 stack에 i 추가

 

comment