理解题意很关键。这道题的重点是说,要找出t中,比s的子串多出来的字符,也就是说,如果s="aaa";t="aaaa",那么 res = “a”;
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
char findTheDifference(string s, string t) {
unordered_map<char, int> um;
for (char ch : s) ++um[ch];
for (char ch : t) {
if (um.count(ch) && um[ch] > 0) --um[ch];
else return ch;
}
}
};
int main() {
Solution so;
string s = "aaa";
string t = "aaaa";
cout << so.findTheDifference(s,t) << endl;
system("pause");
return 0;
}