# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## 2020년 9월 3일 목요일
# ### leetCode - String Compression (Python)
# ### 문제 : https://leetcode.com/problems/string-compression/
# ### 블로그 : https://somjang.tistory.com/entry/leetCode-443-String-Compression-Python

# ### 첫번째 시도

class Solution:
    def compress(self, chars: List[str]) -> int:
        stack = []
        nums = 1
        idx = 0
        
        if len(chars) > 0:
            stack.append(chars[0])
        
        for i in range(1, len(chars)):
            if chars[i] == stack[idx]:
                nums = nums + 1
                
                if i == len(chars) - 1:
                    num_list = list(str(nums))
                    idx = idx + 1 + len(num_list)
                
                    stack = stack + num_list
                    
                
            elif chars[i] != stack[idx]:

                if nums == 1:    
                    idx = idx + 1

                num_list = list(str(nums))
                
                if num_list != ['1']:
                    idx = idx + 1 + len(num_list)
                    stack = stack + num_list
                
                stack.append(chars[i])
                nums = 1

        for i in range(len(stack)):
            chars[i] = stack[i]
        
        chars = chars[0:len(stack)]
        
        return len(chars)

StarcoderdataJupyterScriptsDedupFiltered