1) 첫번째 풀이

def zzz(s,i):
    new_s, reduce,length =  '', 1, len(s)
    while s:
        if s[:i] == s[i:2*i]:
            reduce += 1
        else:
            if reduce==1:
                new_s += s[:i]
            else:
                new_s += f'{reduce}'+s[:i]
            reduce=1
        s = s[i:]
    return len(new_s)

def solution(s):
    if len(s)==1:
        return len(s)
    else:
        return min([zzz(s,i) for i in range( 1, int(len(s)//2)+1 )])

2) 두번째 풀이

def solution(s):
    length = []
    result = ""
    
    if len(s) == 1:
        return 1
    
    for cut in range(1, len(s) // 2 + 1): 
        count = 1
        tempStr = s[:cut] 
        for i in range(cut, len(s), cut):
            if s[i:i+cut] == tempStr:
                count += 1
            else:
                if count == 1:
                    count = ""
                result += str(count) + tempStr
                tempStr = s[i:i+cut]
                count = 1

        if count == 1:
            count = ""
        result += str(count) + tempStr
        length.append(len(result))
        result = ""
    
    return min(length)

'프로그래머스 > [코테]Level2' 카테고리의 다른 글

[Python] 카펫  (0) 2020.11.06
[Python] 구명보트  (0) 2020.11.06
[Python] 큰 수 만들기  (0) 2020.10.23
[Python] 주식가격  (0) 2020.10.23
[Python] 위장  (0) 2020.10.23

+ Recent posts