题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
void fun2(string &str, char c)
{
string::iterator it = str.begin();
while (it != str.end())
{
if (*it == c)
{
str.erase(it);
if (it == str.begin())
{
continue;
}
else
{
it--;
}
}
it++;
}
}
int main()
{
string str;
cin >> str;
string strdata = str;
int length = str.length();
map<char,int> m; //用来储存出现的字符个数
int count ;
//先确定每个字符有多少个
while (strdata.length() != 0)
{
int i = 0;
count = 1;
for (int j = 1; j < strdata.length(); j++)
{
if (strdata[i] == strdata[j])
{
count++;
}
}
m.insert(pair<char, int>(strdata[i], count));
//删除已经找到的 字符
fun2(strdata,strdata[i]);
}
//判断谁是最少的
int icount = 10;
for (auto iter = m.begin(); iter != m.end(); iter++)
{
if ((iter->second )<icount)
{
icount = iter->second;
}
}
//找到最少的元素了 icount
for (auto iter = m.begin(); iter != m.end(); iter++)
{
if ((iter->second)==icount)
{
fun2(str, iter->first);
}
}
cout << str << endl;
system("pause");
return 0;
}

美团工作强度 2569人发布