删除所有出现次数最少的字符

题意: 假设字符串中出现次数最少的字母是x, 出现次数为y, 删除所有出现次数为y的字符
思路:用unordered_map统计出出现次数最少的x出现的次数y

再遍历字符串,删除所有出现次数为y的字符

代码:

#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	string a;
	while (cin >> a)
	{
		unordered_map<char,int> b;//key value
		//将所有的单词存放在map结构中,map会根据key排序 但key不能修改,所有不能作为计数
		for (int i = 0;i < a.size();i++)
		{
			pair<unordered_map<char, int>::iterator, bool> ret;
			ret=b.insert(pair<char, int>(a[i], 1));
			if (!ret.second)
				b[a[i]]++;//map下标访问 用key访问value

		}
		//找到value最小的,即出现次数最少的字母
		int res=100;//最小次数
		for (auto f : b)
		{
			if (f.second <= res)
				res = f.second;

		}
		//删除
		vector<char> c;
		
		for (auto g : b)
		{
			if (g.second == res)
				c.push_back(g.first);

		}
		for (int i = 0;i < a.size();i++)
		{
			bool d = true;
			for (int j = 0;j < c.size();j++)
			{
				if (a[i] == c[j])
				{
					d = false;
					
				}
				
			}
			if (d == true)
				cout << a[i];
			else
				continue;

		}
		
		cout << endl;
	}
}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值