문제
https://school.programmers.co.kr/learn/courses/30/lessons/142086
코드
def solution(s):
answerList = [-1]*len(s)
for i in range(len(s)):
for j in range(i):
if (s[i] == s[j]):
answerList[i] = i-j
return answerList
1. 배열의 길이만큼 리스트를 만들어놓고 다 -1로 초기화해놓는다.
2. 내 앞 인덱스만 확인을 하면 되므로 배열 길이만큼 for문 돌려주고 안에서 그 인덱스만큼 또 for문 돌려준다.
3. i번째 값과 j번째 값이 같으면 i-j (현재 인덱스에서 똑같은 문자 인덱스) 를 해준다.
728x90
'study > 알고리즘' 카테고리의 다른 글
[알고리즘] 그리디 알고리즘이란? (0) | 2024.04.08 |
---|---|
[파이썬] 파이썬 문법 정리 (1) | 2024.04.01 |
[코드업] 코드업 파이썬 기초 100제 (0) | 2024.04.01 |
[프로그래머스] [PCCE 기출문제] 9번 이웃한 칸 (Python) (0) | 2024.02.25 |
[프로그래머스 코딩테스트 입문 100문제] Day 2 사칙연산, 조건문, 배열 (Python) (0) | 2024.02.24 |