
Desicription
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:
1 2 3 4 5 6 7 8 9
|
Input: s = "abcd" t = "abcde"
Output: e
Explanation: 'e' is the letter that was added.
|
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class { public: char findTheDifference(const std::string& s, const std::string& t) { std::vector<int> sCount = std::vector<int>(26, 0); std::vector<int> tCount = std::vector<int>(26, 0);
for(const auto& c : s) { sCount[c - 'a']++; } for(const auto &c : t) { tCount[c - 'a']++; }
int index = 0; for(int i = 0; i < sCount.size(); i++) { if(tCount[i] == sCount[i] + 1) { index = i; break; } }
return index + 'a'; } };
|
近期评论