注意“a” 和 “a” 不是变位词= =。。
func isAnagram(_ s: String, _ t: String) -> Bool {
let m = s.count
let n = t.count
if m != n || m == 1 && n == 1 {
return false
}
var dict = Dictionary<Character,Int>()
for c in s {
if let count = dict[c] {
dict[c] = count + 1
}
else{
dict[c] = 1
}
}
for c1 in t {
if let count = dict[c1]{
dict[c1] = count - 1
if count - 1 == 0 {
dict.removeValue(forKey: c1)
}
}
}
if dict.keys.count == 0 {
return true
}else {
return false
}
}
网友评论