当前位置:网站首页>leetcode 1647. Minimum deletions to make character frequencies unique
leetcode 1647. Minimum deletions to make character frequencies unique
2022-06-28 18:54:00 【Blue feather bird】
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”.
The frequency of each letter refers to its frequency in the string s Is the number of times ,
To make a string s The frequency of each letter is different ,
If the frequency is the same , To delete letters and make them different in frequency , At least delete a few letters .
Ideas :
I usually think of using hash map Record the number of times each letter appears , Then try to delete letters with the same frequency .
Because the letters only have 26 individual , So let's use a int Array representation hash map.
If the frequency is out of sequence , It will be much easier to handle , It only needs to vary each frequency from large to small 1 That's all ,
If the frequencies are equal, reduce the frequency to make their frequencies differ 1 above .
Note that if the frequencies are 0, There is no need to reduce the frequency , Because there is nothing to lose without this letter .
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;
}
边栏推荐
- Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years
- Michael Wooldridge, professeur à l'Université d'Oxford: comment la communauté de l'IA voit les réseaux neuronaux depuis près de 40 ans
- 牛津大學教授Michael Wooldridge:AI社區近40年如何看待神經網絡
- 新工作第一天
- 微信小程序接入百度统计报错 Cannot read property ‘mtj‘ of undefined
- About Covariance and Correlation(协方差和相关)
- 上传文件列表(文件名重复加括号标识)
- 基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
- 双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
- Analyzing the practical development of robot teaching
猜你喜欢
随机推荐
Anonymous function this pointing and variable promotion
【软件测试】2022年普通高等学校招生全国统一考试
First day of new work
Understanding of closures
中金财富开户安全吗?开过中金财富的讲一下
Oom out of memory memory overflow
双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
Servlet的使用手把手教学(一)
WiFi安全漏洞KRACK深度解读
匿名函数变量问题
C语言指针的一些易错点
Advanced technology management - how managers communicate performance and control risks
Chapter 2 processing files, cameras and GUI Cameo applications
百度时间因子添加
Business layer modification - reverse modification based on the existing framework
Upload file list (repeated file names are marked with brackets)
leetcode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers(最少的“二进制数“个数)
Seata implementation of sharing JDBC distributed transaction
C# 41. Int to string
leetcode 1689. Partitioning into minimum number of deci binary numbers









