问题描述:
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.分析:哈希表原理,统计两个字符串中每个字符的个数,然后找出两个字符串哈希表的不同,返回不同的那个字符即可
AC代码:
char findTheDifference(string s, string t)
{
char res;
int hash1[256] = {0};
int hash2[256] = {0};
for(int i = 0;i < s.size();i++)
hash1[s[i]]++;
for(int i = 0;i < t.size();i++)
hash2[t[i]]++;
for(int i = 0;i < 256;i++)
{
if(hash1[i] != hash2[i])
{
res = char(i);
break;
}
}
return res;
}