문제 링크

풀이 과정

전체 코드

def solve(sequence):
    stack, num_110, i = [], 0, 0
    
    while i < len(sequence):
        if sequence[i] == '0':
            if len(stack) >= 2 and stack[-2] == '1' and stack[-1] == '1':
                stack.pop()
                stack.pop()
                num_110 += 1
                i += 1
            else:
                stack.append(sequence[i])
                i += 1
        else:
            stack.append(sequence[i])
            i += 1
            
    find_0_idx = -1  # 0의 위치
    for j in range(len(stack)-1, -1, -1):
        if stack[j] == '0':
            find_0_idx = j
            break
    
    if find_0_idx != -1:
        result = "".join(stack[:find_0_idx+1]) + num_110 * '110' + "".join(stack[find_0_idx+1:])
    else:
        result = num110 * '110' + "".join(stack)
            
    return result
            
def solution(s):
    answer = []
    for sequence in s:
        answer.append(solve(sequence))
        
    return answer