Coding Test Practice/Python

[프로그래머스 코딩테스트 연습 Lv. 1] 가장 가까운 같은 글자

y2r1m 2023. 3. 13. 03:16

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

 

프로그래머스

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

programmers.co.kr

 

def solution(s):
    answer = []
    alp_dict = {}
    for i, alp in enumerate(s):
        
        if alp not in alp_dict.keys():
            answer.append(-1)
        else:
            answer.append(i - alp_dict[alp])
            
        alp_dict[alp] = i # 해당 알파벳이 등장한 가장 최근 인덱스를 저장
        
    return answer