파게로그

[코테] 2020 카카오 공채 (문자열 압축) 본문

콤퓨타 왕왕기초/PS

[코테] 2020 카카오 공채 (문자열 압축)

파게 2020. 10. 16. 22:58

패턴이 10번 이상 반복되면 그 자리수도 고려해야 한다는 점이 엣지 테케였던 것 같다.

while 조건에서 너무 막히면, while True 걸어주고 break하는 것도 고민해보아야겠다...

 

def solution(s):
    def intlength(dup):
        if dup==1: return 0
        cnt = 0
        while dup>9:
            dup //= 10
            cnt += 1
        return cnt+1        
    
    min_compressed_length = len(s)
    
    for length in range(1, len(s)//2 + 1):
        compressed_length = 0
        idx = 0
        cur_pattern = ''
        last_pattern = ''
        dup = 1
        
        while True:
            cur_pattern = s[idx:idx+length]
            if idx != 0: # exclude first pattern
                if cur_pattern == last_pattern:
                    dup += 1
                else:
                    compressed_length += (length+intlength(dup))     
                    dup = 1
            last_pattern = cur_pattern            
            idx += length
            if idx > len(s) - length + 1:
                compressed_length += (length+intlength(dup))
                break
        compressed_length += len(s) - idx
        
        min_compressed_length = min(compressed_length, min_compressed_length)
            
    answer = min_compressed_length
    return answer
Comments