原文链接: https://leetcode-cn.com/problems/minimum-deletions-to-make-character-frequencies-unique
英文原文
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".
Example 3:
Input: s = "ceabaacb" Output: 2 Explanation: You can delete both 'c's resulting in the good string "eabaab". Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).
Constraints:
1 <= s.length <= 105scontains only lowercase English letters.
中文题目
如果字符串 s 中 不存在 两个不同字符 频次 相同的情况,就称 s 是 优质字符串 。
给你一个字符串 s,返回使 s 成为 优质字符串 需要删除的 最小 字符数。
字符串中字符的 频次 是该字符在字符串中的出现次数。例如,在字符串 "aab" 中,'a' 的频次是 2,而 'b' 的频次是 1 。
示例 1:
输入:s = "aab"
输出:0
解释:s 已经是优质字符串。
示例 2:
输入:s = "aaabbbcc" 输出:2 解释:可以删除两个 'b' , 得到优质字符串 "aaabcc" 。 另一种方式是删除一个 'b' 和一个 'c' ,得到优质字符串 "aaabbc" 。
示例 3:
输入:s = "ceabaacb" 输出:2 解释:可以删除两个 'c' 得到优质字符串 "eabaab" 。 注意,只需要关注结果字符串中仍然存在的字符。(即,频次为 0 的字符会忽略不计。)
提示:
1 <= s.length <= 105s仅含小写英文字母
通过代码
高赞题解
首先统计各个字母的出现个数,再使用HashSet进行去重。
HashSet中保存不同的数目,如果加进来的数目已经存在,就自减,减到HashSet中没有的数目
为什么不用排序?例如添加顺序为4 4 3 2 1和3 2 1 4 4,
第一种是把4 3 2 1每个数都减1,答案为4。
第二种是直接把最后一个4减到0,答案也是4.
所以答案只需要在乎去重自减时,减少的个数,而不用在意顺序
下面是代码:
class Solution {
public int minDeletions(String s) {
int[] a = new int[26];
char[] cs = s.toCharArray();
for (char c : cs) a[c - 'a'] ++;// 统计字母个数
Set<Integer> h = new HashSet<Integer>();
int res = 0;
for (int i : a) {
if (i != 0) { // 有数目才进行判断
while (h.contains(i)) { // set已经包含就自减
i --;
res ++;
}
if (i != 0) h.add(i); // 自减到0时,表示完全删除了某个字母,不能加入set中
}
}
return res;
}
}
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 7727 | 14960 | 51.7% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|