자료구조 & 알고리즘 & cs/CodingTest

[python] 프로그래머스 Lv2. - [2018 KAKAO] 압축

뽀또롱 2023. 6. 15. 13:08

코딩테스트 연습 - [3차] 압축 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

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

programmers.co.kr

 

from string import ascii_uppercase
def solution(msg):
    alpa=[i for i in ascii_uppercase]
    answer = []
    
    i=0
    while msg:
        tmp=msg[i]
        
        for j in range(i+1, len(msg)):
            if tmp+msg[j] in alpa:
                tmp+=msg[j]
            else:
                alpa.append(tmp+msg[j])
                break
        
        answer.append(alpa.index(tmp)+1)
        msg=msg[len(tmp):]
        
    return answer

 

문자열 구현 문제였다. 

알파벳 리스트 생성을 한뒤 조건을 만족시키며 그대로 코드를 짜면 되는데

슬라이싱을 사용하는 등 시간 초과 걱정이 있었는데

잘 통과했다.

 

 

* 모든 코드는 github에서 관리합니다.  seinShin (SEIN SHIN) (github.com)

 

seinShin - Overview

blog : https://bboddorong.tistory.com/. seinShin has 7 repositories available. Follow their code on GitHub.

github.com

 

728x90