Algorithm

SwiftUI: 둘만의 암호

songmoro 2023. 12. 17. 22:30
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