美文网首页
1347. Minimum Number of Steps to

1347. Minimum Number of Steps to

作者: 是嘤嘤嘤呀 | 来源:发表于2020-04-13 17:54 被阅读0次
    image.png

    思路: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;
        }
    };
    

    相关文章

      网友评论

          本文标题:1347. Minimum Number of Steps to

          本文链接:https://www.haomeiwen.com/subject/atpnmhtx.html