【題目描述】
給定兩個字符串 s 和 t务热,它們只包含小寫字母。
字符串 t 由字符串 s 隨機重排雀扶,然后在隨機位置添加一個字母蚁孔。
請找出在 t 中被添加的字母屑埋。
示例:
輸入:
s = "abcd"
t = "abcde"
輸出:
e
解釋:
'e' 是那個被添加的字母豪筝。
【思路1】看看就行啦 ,哈哈哈
func findTheDifference(_ s: String, _ t: String) -> Character {
var tmp = t
for cha in s {
let index = tmp.index(of: cha)!
tmp.remove(at: index)
}
return tmp.last!
}
【思路2】
還是使用位運算雀彼,swift語法要熟練壤蚜!
func findTheDifference(_ s: String, _ t: String) -> Character {
var result : uint = 0
for c in s.unicodeScalars {
let tmp = c.value
result ^= tmp
}
for cc in t.unicodeScalars {
let tmp = cc.value
result ^= tmp
}
return Character(UnicodeScalar(result)!)
}