删除字符串中出现次数最少的字符(HJ23)

一:解题思路

二:完整代码示例 (C++版和Java版)

C++代码:

#include <iostream>
#include <vector>
#include <string>
#include <map>

using namespace std;

int main()
{
    string str = "";

    while (cin >> str)
    {
        map<int,int> map;
        int minValue = 2147483647;
        for (int i = 0; i < str.size(); i++)
        {
            int newCount = map[str[i]]+1;
            if (newCount < minValue)
            {
                minValue = newCount;
            }

            map[str[i]] = newCount;
        }

        for (int i = 0; i < str.size(); i++)
        {
            if (minValue != map[str[i]])
            {
                cout << str[i];
            }
        }
        cout << endl;
    }

    return 0;
}

 

posted @ 2020-08-06 21:01  repinkply  阅读(349)  评论(0)    收藏  举报