Skip to content

[Algorithm] 가장 가까운 같은 글자 #35

Description

@hwangJi-dev

💬 문제

[코딩테스트 연습 - 가장 가까운 같은 글자](https://school.programmers.co.kr/learn/courses/30/lessons/142086)

💬 Idea

  1. dict에 글자의 지난 index를 저장해두고 해당 글자가 다시 나오면 최신 index에서 지난 index를 빼서 index의 차이를 구하자.

💬 풀이

func solution(_ s:String)->[Int]{varwordIndexDict:[String:Int]=[:]varresult:[Int]=[]for(index, i)in s.enumerated(){letword=String(i)ifwordIndexDict[word]!=nil{
result.append(index - wordIndexDict[word]!)}else{
result.append(-1)}wordIndexDict[word]= index
}return result
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions