website/content/ChapterFour/0300~0399/0389.Find-the-Difference.md
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
Input:
s = "abcd"
t = "abcde"
Output:
e
Explanation:
'e' is the letter that was added.
给定两个字符串 s 和 t,它们只包含小写字母。字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。请找出在 t 中被添加的字母。
X^X = 0,将 s 和 t 依次异或,最终多出来的字符就是最后异或的结果。
package leetcode
func findTheDifference(s string, t string) byte {
n, ch := len(t), t[len(t)-1]
for i := 0; i < n-1; i++ {
ch ^= s[i]
ch ^= t[i]
}
return ch
}