Skip to content

[Algorithm] 둘만의 암호 #105

Description

@hwangJi-dev

💬 문제

[코딩테스트 연습 - 둘만의 암호](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{varabc=["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)}varans=""foriin s {
ans +=abc[(abc.firstIndex(of:String(i))! + index)% abc.count]}return ans
}

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