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.首先想到的是用位操作或者是string的操作函数实现,不过后者相对麻烦些,所以采用的前者
利用异或的性质,相同位返回0,这样相同的字符都抵消了,剩下的就是后加的那个字符
class Solution {
public:
char findTheDifference(string s, string t) {
char res = 0;
for (char c : s) res ^= c;
for (char c : t) res ^= c;
return res;
}
};