본문 바로가기
Algorithm

SwiftUI: 둘만의 암호

by songmoro 2023. 12. 17.
728x90

알파벳에서 skip에 포함되는 알파벳을 제거한 후, 배열의 각 원소에서 index만큼 이동한 알파벳을 반환해줍니다.

 

func solution(_ s:String, _ skip:String, _ index:Int) -> String {
    var result = ""
    
    var alphabet = (97...122).map {
        Character(UnicodeScalar($0))
    }
    alphabet.removeAll { skip.contains($0) }
    
    let alphabetDict = Dictionary(uniqueKeysWithValues: zip(alphabet, 0..<alphabet.count))

    s.forEach { char in
        result.append(alphabet[(alphabetDict[char]! + index) % alphabet.count])
    }

    return result
}
728x90

'Algorithm' 카테고리의 다른 글

SwiftUI: 신고 결과 받기  (0) 2023.12.17
SwiftUI: 신규 아이디 추천  (0) 2023.12.17
SwiftUI: 카드 뭉치  (0) 2023.12.17
SwiftUI: 대충 만든 자판  (0) 2023.12.17
SwiftUI: 덧칠하기  (1) 2023.12.17