def solution(word):
answer = []
checkString = ""
for w in word:
if w in checkString:
location = checkString.index(w)
answer.append(location + 1)
else:
answer.append(-1)
checkString = w + checkString
return answer
Lv1에서는 문자열 관련 문제들이 많은 것 같다. checkString에 문자를 역순으로 더해가면서 가장 가까운 문자의 index를 반환하는 방법으로 해결했다.
def solution(s):
answer = []
word_dict = {}
for idx, word in enumerate(list(s)):
if word not in word_dict:
answer.append(-1)
word_dict[word] = idx
else:
answer.append(idx - word_dict[word])
word_dict[word] = idx
return answer
다른 사람 풀이를 한번 봤는데 dictionary를 통해서 각 문자의 최신 index를 저장해놓는 방법으로 해결했다.