본문 바로가기
Algorithms/Programmers

Programmers - 문자열 압축

by Pinocchio 2021. 8. 29.

My solution in Python for 'Programmers-popping a ballon' 

Requests for explanation or comments on the code are always welcome.

 

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

 

코딩테스트 연습 - 문자열 압축

데이터 처리 전문가가 되고 싶은 "어피치"는 문자열을 압축하는 방법에 대해 공부를 하고 있습니다. 최근에 대량의 데이터 처리를 위한 간단한 비손실 압축 방법에 대해 공부를 하고 있는데, 문

programmers.co.kr

 

A. https://github.com/Ihyun/Algorithms/blob/master/Programmers/string%20compression.py

 

GitHub - Ihyun/Algorithms

Contribute to Ihyun/Algorithms development by creating an account on GitHub.

github.com

 

# Programmers - 문자열 압축
# https://programmers.co.kr/learn/courses/30/lessons/60057


def solution(s):
    answer = 0
    sol = len(s)
    dic = {}

    for i in range(1, len(s) // 2 + 1):
        tempstr = s[0:i]
        tempsol = i + len(s) % i
        tempcnt = 1
        for j in range(i, len(s) + 1 - i, i):
            if tempstr != s[j:j + i]:
                tempstr = s[j:j + i]
                tempsol += i
                if tempcnt > 1:
                    tempsol += len(str(tempcnt))
                tempcnt = 1
            else:
                tempcnt += 1
        if tempcnt > 1:
            tempsol += len(str(tempcnt))
        if tempsol < sol:
            sol = tempsol

    return sol

'Algorithms > Programmers' 카테고리의 다른 글

Popping a balloon  (0) 2021.08.29
Programmers - camouflage  (0) 2021.08.29
Programmers - triangular snail  (0) 2021.08.18

댓글