[알고리즘/백준] 4889번 : 안정적인 문자열(python)

유현민·2022년 3월 8일
0

알고리즘

목록 보기
38/253

스택을 이용하여 문제를 풀었다.

def solution():
    idx = 1
    while True:
        word = list(map(str, input()))
        if '-' in word:
            break
        stack = list()
        cnt = 0
        for i in word:
            if i == '{':
                stack.append(i)
            elif stack:
                stack.pop()
            else:
                cnt += 1
                stack.append('{')

        cnt += len(stack) // 2
        print(f'{idx}. {cnt}')
        idx += 1


if __name__ == "__main__":
    solution()
profile
smilegate megaport infra

0개의 댓글