题目:
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.
思路:
字符串t在字符串s的基础上多加了一个字符,要求我们找出那个字符。将s和t当作一个整体,除了一个字符其余字符都出现了两次,即可对s和t的每个字符进行异或操作,最后的结果就是单独的那个字符。
程序:
class Solution {
public:
char findTheDifference(string s, string t) {
char res = 0x00;
for(int i = 0;i < s.size();i++)
res ^= s[i];
for(int i = 0;i < t.size();i++)
res ^= t[i];
return res;
}
};