Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hwangJi-dev opened this issue Dec 13, 2022 · 0 comments
Closed

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

hwangJi-dev opened this issue Dec 13, 2022 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Dec 13, 2022

💬 문제

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

💬 Idea

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

💬 풀이

func solution(_ s:String) -> [Int] {
    var wordIndexDict: [String: Int] = [:]
    var result: [Int] = []
    
    for (index, i) in s.enumerated() {
        let word = String(i)
        
        if wordIndexDict[word] != nil {
            result.append(index - wordIndexDict[word]!)
        } else {
            result.append(-1)
        }
        
        wordIndexDict[word] = index
    }
    
    return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant