[구현] 문자열 압축

라다디·2021년 4월 10일
0

🍏 문제


https://programmers.co.kr/learn/courses/30/lessons/60057

📄 코드


더 효율적인 코드를 생각해 볼 것

def solution(s):
    len_list = [len(s) + 1] * (len(s) + 1)
    for i in range(1, len(s) + 1):
        w = s[0:i]
        same = count = 1
        short = ""
        for j in range(i, len(s) + 1, i):
            if w != s[j:j + i]:
                same = 0

            if same == 0:
                if count == 1:
                    short += w
                else:
                    short += str(count) + w
                same = count = 1
            else:
                count += 1
            w = s[j:j + i]

            if j + i >= len(s):
                if j + i == len(s):
                    if count == 1:
                        short += w
                    else:
                        short += str(count) + w
                elif j + i > len(s):
                    short += s[j:]
                len_list[i] = len(short)
                break

    return min(len_list)

✍ 풀이


profile
Every day can be the beginning of a new life

2개의 댓글