본문 바로가기
프로그래머스

Level2 문자열 압축

by ysunee 2022. 1. 25.
def solution(s):
    n = len(s)
    result = 0; min = 100000
    for i in range(1,n+1):
        
        a=0; sum=0; count=0
        while True:
            if(a+2*i>n): 
                if(count!=0): sum+=count*i-1
                if(count>=999): sum-=3
                elif(count>=99): sum-=2
                elif(count>=9): sum-=1
                break
                
            if(s[a:a+i]==s[a+i:a+2*i]): count+=1; a+=i
            else: 
                if(count!=0): sum+=count*i-1
                if(count>=999): sum-=3
                elif(count>=99): sum-=2
                elif(count>=9): sum-=1    
                count=0; a+=i    
        result=n-sum;
        if(result<min): min= result
    
    return min

 

완전 탐색 문제, 문자열 문제

 

<알아둘 것>

s = "hello word"

s[0:5] = "hello"

'프로그래머스' 카테고리의 다른 글

Level2 짝지어 제거하기  (0) 2022.01.27
Level2 124 나라의 숫자  (0) 2022.01.27
Level2 멀쩡한 사각형  (0) 2022.01.27
Level2 오픈채팅방  (0) 2022.01.26
[파이썬] 깊이/너비 우선 탐색(DFS/BFS) 네트워크  (0) 2021.11.26

댓글