思路:step1:用两个map分别存储两个串字符重复的个数;
step2:比较两个key相同的值的差异,如果map_s[k] > map_t[k],叠加;
代码:
class Solution {
public:
map <char, int> getMap(string str) {
map<char, int> res;
for (int i = 0; i < str.length(); i++) {
if (res.count(str[i])) res[str[i]] ++;
else res[str[i]] = 1;
}
return res;
}
int minSteps(string s, string t) {
int res = 0;
map <char, int> map_s = getMap(s);
map <char, int> map_t = getMap(t);
map <char, int>::iterator iter = map_s.begin();
while(iter != map_s.end()) {
map_t.count(iter -> first) ?: map_t[iter -> first] = 0;
cout << map_t[iter -> first] << iter -> second;
if (iter -> second > map_t[iter -> first]) res += iter -> second - map_t[iter -> first];
iter ++;
}
return res;
}
};
网友评论