当前位置:网站首页>leetcode 1647. Minimum Deletions to Make Character Frequencies Unique(所有字母频率不同的最小删除次数)
leetcode 1647. Minimum Deletions to Make Character Frequencies Unique(所有字母频率不同的最小删除次数)
2022-06-28 18:30:00 【蓝羽飞鸟】
A string s is called good if there are no two different characters in s that have the same frequency.
Given a string s, return the minimum number of characters you need to delete to make s good.
The frequency of a character in a string is the number of times it appears in the string. For example, in the string “aab”, the frequency of ‘a’ is 2, while the frequency of ‘b’ is 1.
Example 1:
Input: s = “aab”
Output: 0
Explanation: s is already good.
Example 2:
Input: s = “aaabbbcc”
Output: 2
Explanation: You can delete two 'b’s resulting in the good string “aaabcc”.
Another way it to delete one ‘b’ and one ‘c’ resulting in the good string “aaabbc”.
每个字母的频率指的是它在字符串s中出现的次数,
要让字符串s中每个字母的频率都不一样,
如果频率一样,要删除字母使它们频率不一样,至少要删几个字母。
思路:
一般会想到用hash map记录每个字母出现的次数,然后再想办法删掉频率一样的字母。
因为字母只有26个,所以用一个int数组代表hash map。
如果频率排过序,处理起来会方便很多,只需要从大到小每个频率相差1以上就行,
频率相等的就减频率使它们的频率相差1以上。
注意如果频率都是0,是不需要减频率的,因为都没有这个字母还减什么呢。
public int minDeletions(String s) {
int[] cnt = new int[26];
int cur = 0;
int res = 0;
for(char ch : s.toCharArray()) cnt[ch - 'a'] ++;
Arrays.sort(cnt);
cur = cnt[25];
for(int i = 24; i >= 0; i --) {
if(cnt[i] > cur - 1) {
res += cnt[i] - Math.max(cur - 1, 0);
cur --;
} else {
cur = cnt[i];
}
}
return res;
}
边栏推荐
- Mycat+ sub database and sub table
- Go, begin, end, for, after, instead of
- tensorboard 使用总结
- Go 降序排序 取 Top N
- Small program graduation project based on wechat campus lost and found graduation project opening report function reference
- Architecture autonomy service: building data-driven architecture insight
- 解析机器人主持教学的实践发展
- 启牛学堂的vip证券账户是真的安全正规吗?怎么说
- 实时Transformer:美团在单图像深度估计上的研究
- BioVendor游离轻链(κ和λ)Elisa 试剂盒检测步骤
猜你喜欢

独立站卖家如何高效管理复杂的Facebook主页?

Mycat+分库分表

NFT流动性协议的安全困局—NFT借贷协议XCarnival被黑事件分析

Small program graduation project based on wechat agricultural and sideline products agricultural products mall small program graduation project opening report function reference

GCC getting started manual

PCB线路板布局和布线都有哪些设计要求?

Shanghai Pudong Development Bank Software Test interview real question

Small program graduation project based on wechat chess and card room small program graduation project opening report function reference

IDM certification process log embedding point description

1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import
随机推荐
id门禁卡复制到手机_怎么把手机变成门禁卡 手机NFC复制门禁卡图文教程
如何高效优雅地管理接口文档
Architecture autonomy service: building data-driven architecture insight
中金财富开户安全吗?开过中金财富的讲一下
tensorboard 使用总结
图形系统——1. 布局加载
请问大智慧上开户安全吗
A preliminary study of IO model
Lumiprobe非荧光炔烃研究丨DBCO NHS 酯
Cann media data processing V2, jpegd interface introduction
数据库对比工具
抗兔Dylight 488丨Abbkine通用型免疫荧光(IF)工具箱
东方财富软件股票开户是靠谱的吗?在哪开户安全
浦发银行软件测试面试真题(小编面试亲测)
PHP使用栈解决迷宫问题
How to use the current conversion function in SAP CDs view
An error is reported when ActiveMQ is started. The 1883 port occupation problem is solved
Easyexcel learning notes
Is it reliable to open an account for the shares of Oriental Wealth software? Where is safe to open an account
今天开户今天能买股票吗?在线开户是安全么?