ji-0630 / CodingTest

코딩테스트 연습 기록
0 stars 0 forks source link

가장 가까운 같은 글자 #264

Closed ji-0630 closed 1 year ago

ji-0630 commented 1 year ago

문제 설명

image

ji-0630 commented 1 year ago

나의 풀이

def solution(s):
    answer = []
    for i in range(len(s)):
        if s[:i][::-1].find(s[i]) == -1:
            answer.append(s[:i][::-1].find(s[i]))
        else:
            answer.append(s[:i][::-1].find(s[i])+1)

    return answer
ji-0630 commented 1 year ago

다른 사람의 풀이

def solution(s):
    answer = []
    dic = dict()
    for i in range(len(s)):
        if s[i] not in dic:
            answer.append(-1)
        else:
            answer.append(i - dic[s[i]])
        dic[s[i]] = i

    return answer