ji-0630 / CodingTest

코딩테스트 연습 기록
0 stars 0 forks source link

짝지어 제거하기 #236

Open ji-0630 opened 1 year ago

ji-0630 commented 1 year ago

문제 설명

image

ji-0630 commented 1 year ago

나의 풀이

첫 번째

def solution(s):
    i = 0
    while len(s) > i+1:
        if s[i] == s[i+1]:
            s = s[:i]+s[i+2:]
            i=0
        else:
            i += 1
    if len(s) == 0:
        return 1
    else:
        return 0

두 번째

def solution(s):
    i = 0
    while len(s)-1 > i:
        if s[i] == s[i+1]:
            s = s[:i]+s[i+2:]
            i = max(0, i-1)
        else:
            i += 1

    if len(s) == 0:
        return 1
    else:
        return 0
ji-0630 commented 1 year ago

다른 사람의 풀이

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