給定兩個字符串 s 和 t,它們只包含小寫字母。
字符串 t 由字符串 s 隨機(jī)重排,然后在隨機(jī)位置添加一個字母。
請找出在 t 中被添加的字母。
示例:
輸入:
s = "abcd"
t = "abcde"
輸出:
e
解釋:
'e' 是那個被添加的字母。
題解:
兩個數(shù)組,分別進(jìn)行計數(shù),然后最后找,誰多了一個,那就是誰重復(fù)了
public:
char findTheDifference(string s, string t) {
vector<int> hash(26,0);
vector<int> hash1(26,0);
for(auto x : s){
hash[x - 'a'] ++;
}
for(auto x : t){
hash1[x - 'a'] ++;
}
for(int i = 0 ; i < 26 ; i++){
if(hash[i] != hash1[i]) return 'a' + i;
}
return 'o';
}
};