题目:
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.
思路:
ans初始值为0,然后对s和t的所有元素进行异或操作后就是答案。
代码实现:
class Solution {
public:
char findTheDifference(string s, string t) {
int ans = 0;
for (int i = 0; i < s.size(); ++i){
ans ^= s[i];
}
for (int i = 0; i < t.size(); ++i){
ans ^= t[i];
}
return ans;
}
};