[백준] 20310. 타노스

원숭2·2022년 3월 5일
0

백준

목록 보기
53/54

문제

풀이

  1. 문자열의 순서를 유지하면서 절반을 날려야하기 때문에, 0은 앞에서부터 제거하고 1은 뒤에서 부터 제거해줌.
  2. 총 제거 횟수는 count 함수를 통해 찾아냈으며, pop(index)를 활용하여 해당 자리의 숫자를 제거함.

코드

def solution() :
    n = list(input())
    zero = n.count('0') // 2
    one = n.count('1') // 2
    
    for _ in range(zero) :
        n.pop(-(n[::-1].index('0'))-1)
    
    for _ in range(one) :
        n.pop(n.index('1'))
    
    print(''.join(n))
    
solution()

0개의 댓글