[프로그래머스] 문자열 압축

rhkr9080·2023년 8월 5일
0

프로그래머스

목록 보기
6/19

문제링크 : https://school.programmers.co.kr/learn/courses/30/lessons/60057#

💻 문제 풀이 : Python

😁 구현해서 풀기... 보완이 필요할듯!
# 몇 번 반복되는지 카운트해야함
# 길이의 최소값을 리턴
def solution(s):
    answer = 0
    minTmp = len(s)
    # 1부터 len/2 까지 자르기 가능
    for i in range(1, len(s)//2 + 1):
        tmpList = []
        for j in range(0, len(s), i):
            tmpList.append(s[j:j+i])
        print(tmpList)
        tmpStr = ""
        tmpCnt = 1
        for j in range(0, len(tmpList) - 1):
            if(tmpList[j] == tmpList[j+1]):
                tmpCnt += 1
                if(j+1 == len(tmpList) -1):
                    tmpStr += str(tmpCnt)
                    tmpStr += tmpList[j]
            else:
                if tmpCnt > 1:
                    tmpStr += str(tmpCnt)
                tmpStr += tmpList[j]
                tmpCnt = 1
                if(j+1 == len(tmpList) -1):
                    tmpStr += tmpList[j+1]
        print(tmpStr)
        minTmp = min(minTmp, len(tmpStr))
        
    answer = minTmp
    return answer

ref)


📌 memo

  • range(start, end, step)
profile
공부방

1개의 댓글

comment-user-thumbnail
2023년 8월 5일

많은 도움이 되었습니다, 감사합니다.

답글 달기