코드는 Swift로 작성하였습니다.
제가 직접 작성한 코드라 최적의 코드가 아닐 수 있음을 알려드립니다. 🐹
문제는 저작권 관련 문제가 있을 수 있어서 적어두지 않았습니다.
import Foundation
func solution(_ s:String, _ n:Int) -> String {
var result = ""
for i in s {
var unicode = (i.asciiValue!)
if i == " " {
result += " "
}
else {
if unicode >= 65 && unicode <= 90 {
if unicode + UInt8(n) > 90 {
unicode -= 26
}
}
else {
if unicode + UInt8(n) > 122 {
unicode -= 26
}
}
result += String(UnicodeScalar((unicode) + UInt8(n)))
}
}
return result
}
케이스로 나눠서 풀면 좀 더 깔끔할 것 같다.