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

2022. 8. 12. 17:28·CodingTest/Programmers

0. 문제 링크

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. 문제 설명

1) 배열 자르기

2) 비교 문자열과 반복 횟수 구하기

3) 합친 문자열에서 최소값 구하기

 

2. 입출력

# input
s = 'abcabcdede'

# output
answer = 8

 

3. 코드

1) 첫 번째 코드

def solution(s):
    compare_min = len(s)                                # 최소값

    for length in range(1, len(s)):                     # 1부터 문자열 개수만큼 자르기
        # 1. 배열 잘라서 temp에 넣기
        temp = []                                       # 임시 배열
        for j in range(0, len(s), length):
            temp.append(s[j:j+length])

        # 2. 비교 문자열과 반복 횟수 구하기
        compare_string = temp[0]                        # 비교 문자열 (초기값은 첫 번째 문자로)
        compare_count = 1                               # 반복 횟수 (초기값은 1로)
        compare_list = []                               # 개수와 문자열 추가
        for k in range(1, len(temp)):
            if compare_string == temp[k]:               # 같은 경우
                compare_count += 1                      # count 1 증가
            else:                                       # 다른 경우
                if compare_count == 1:                  # count가 1인 경우
                    compare_list.append(compare_string) # compare_string 한 번 정답에 추가
                else:                                   # count가 2 이상인 경우
                    compare_list.append(str(compare_count) + compare_string)
                    compare_count = 1
                                                        # compare_count와 compare_string 한 번 정답에 추가
                compare_string = temp[k]                # 비교 문자열 바꾸기
        if compare_count == 1:  # count가 1인 경우
            compare_list.append(compare_string)  # compare_string 한 번 정답에 추가
        else:  # count가 2 이상인 경우
            compare_list.append(str(compare_count) + compare_string)

        # 3. 전체 문자열 수에서 최소값 구하기
        final_string = ''.join(compare_list)
        if compare_min > len(final_string):
            compare_min = len(final_string)

    return compare_min

 

실행 결과

 

4. 알게된 점

 

5. Github

https://github.com/iihye/Algorithm/blob/main/Programmers/compress_string.py

 

GitHub - iihye/Algorithm

Contribute to iihye/Algorithm development by creating an account on GitHub.

github.com

 

728x90
저작자표시 비영리 변경금지 (새창열림)

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

[프로그래머스]멀쩡한 사각형(Python)  (0) 2022.08.19
[프로그래머스]오픈채팅방(Python)  (0) 2022.08.16
[프로그래머스]K번째수(Python)  (0) 2022.08.11
[프로그래머스]완주하지 못한 선수(Python)  (0) 2022.08.10
[프로그래머스]폰켓몬(Python)  (0) 2022.08.09
'CodingTest/Programmers' 카테고리의 다른 글
  • [프로그래머스]멀쩡한 사각형(Python)
  • [프로그래머스]오픈채팅방(Python)
  • [프로그래머스]K번째수(Python)
  • [프로그래머스]완주하지 못한 선수(Python)
iihye_
iihye_
  • iihye_
    hye-log
    iihye_
    • 분류 전체보기 (296)
      • Development (19)
        • Spring (4)
        • Python (5)
        • React (4)
        • Next.js (1)
        • JavaScript (4)
        • CSS (1)
      • Infra (15)
        • Docker (1)
        • Jenkins (2)
        • Nginx (3)
        • JBoss (1)
        • Windows (3)
        • Linux (3)
        • Kafka (2)
      • Database (1)
        • MongoDB (1)
      • Tools (2)
        • Git (0)
        • Github (1)
        • Document (1)
      • CodingTest (125)
        • Programmers (38)
        • Baekjoon (52)
        • SWEA (27)
        • Jungol (4)
        • Codetree (1)
        • Goorm (3)
      • Education (99)
        • SSAFY 10기 (5)
        • AI Tech 4기 (94)
      • NomadCoder (35)
        • (JS)크롬 앱 만들기 (35)
      • Notice (0)
  • 링크

    • Github
  • 인기 글

  • 최근 글

  • 전체
    오늘
    어제
  • hELLO· Designed By정상우.v4.10.5
iihye_
[프로그래머스]문자열 압축(Python)
상단으로

티스토리툴바