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] 둘만의 암호 #105

Closed
hwangJi-dev opened this issue Feb 4, 2023 · 0 comments
Closed

[Algorithm] 둘만의 암호 #105

hwangJi-dev opened this issue Feb 4, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Feb 4, 2023

💬 문제

[코딩테스트 연습 - 둘만의 암호](https://school.programmers.co.kr/learn/courses/30/lessons/155652)


💬 Idea

  1. abc 배열에서 건너뛸 문자들을 필터링한다
  2. s를 돌면서 abc 배열에서 해당 위치를 찾고, index를 더하여 idx를 도출한다
  3. idx가 abc의 카운트보다 클 수가 있으므로 해당 경우를 방지하기 위해 abc.count만큼 나눈 나머지를 index로 사용한다.

💬 풀이

func solution(_ s:String, _ skip:String, _ index:Int) -> String {
    var abc = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"].filter{ !skip.contains($0) }
    var ans = ""
    
    for i in s {
        ans += abc[(abc.firstIndex(of: String(i))! + index) % abc.count]
    }
    
    return ans
}
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