题解 | #删除字符串中出现次数最少的字符#

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

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;
}

全部评论

相关推荐

想踩缝纫机的小师弟练...:不理解你们这些人,要放记录就把对方公司名字放出来啊。不然怎么网暴他们
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务