프로그래머스/Lv.1
가장 가까운 같은 글자 - Pyton, not in
아찌방
2024. 11. 4. 23:21
https://school.programmers.co.kr/learn/courses/30/lessons/142086
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
풀이 - 나의 생각
딕셔너리에 not in 이란게 있는 줄 몰라서
처음에는 get을 써서 확인 했었다.
좋네
코드
def solution(s):
answer = []
alps = {}
for idx, alp in enumerate(s):
if alp not in alps:
answer.append(-1)
else:
answer.append(idx - alps[alp])
alps[alp] = idx
return answer
def solution(s):
answer = []
alps = {}
for idx, alp in enumerate(s):
if alps.get(alp, 0) == 0:
answer.append(-1)
else:
answer.append(idx - alps[alp] + 1)
alps[alp] = idx + 1
return answer
728x90