문제링크
class Solution {
func wordPattern(_ pattern: String, _ s: String) -> Bool {
var arr: [Int: String] = [:]
let arrPattern = pattern.map { $0 }
let arrS = s.split(separator: " ")
if pattern.count != arrS.count {
return false
}
for (i, c) in arrPattern.enumerated() {
let count: Int = Int(c.asciiValue! - Character("a").asciiValue!)
if !arr.contains(where: { $0.0 == count }) &&
!arr.contains(where: { $0.1 == arrS[i] })
{
arr[count] = String(arrS[i])
} else {
if arr[count] != String(arrS[i]) {
return false
}
}
}
return true
}
}