当前位置:网站首页>LeetCode 1647. Minimum deletion times of unique character frequency
LeetCode 1647. Minimum deletion times of unique character frequency
2022-07-03 22:08:00 【Daylight629】
1647. Character frequency the minimum number of deletions that are unique
If the string s in non-existent Two different characters The frequency of The same thing , It's called s yes High quality string .
Give you a string s, Return to make s Become High quality string Need to delete Minimum Number of characters .
Of characters in a string The frequency of Is the number of occurrences of this character in the string . for example , In string "aab" in ,'a' The frequency of 2, and 'b' The frequency of 1 .
Example 1:
Input :s = "aab"
Output :0
explain :s It is already a high-quality string .
Example 2:
Input :s = "aaabbbcc"
Output :2
explain : You can delete two 'b' , Get a good string "aaabcc" .
Another way is to delete a 'b' And a 'c' , Get a good string "aaabbc" .
Example 3:
Input :s = "ceabaacb"
Output :2
explain : You can delete two 'c' Get a good string "eabaab" .
Be careful , Just focus on the characters that still exist in the result string .( namely , The frequency is 0 Characters are ignored .)
Tips :
1 <= s.length <= 105sSmall letters only
Two 、 Method 1
Hash + greedy
class Solution {
public int minDeletions(String s) {
int[] cnt = new int[26];
Set<Integer> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
cnt[s.charAt(i) - 'a']++;
}
int res = 0;
for (int i = 0; i < 26; i++) {
while(cnt[i] > 0 && !set.add(cnt[i])) {
cnt[i]--;
res++;
}
}
return res;
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(n).
边栏推荐
- WiFi 2.4g/5g/6g channel distribution
- 2022 free examination questions for safety management personnel of hazardous chemical business units and reexamination examination for safety management personnel of hazardous chemical business units
- Pengcheng cup Web_ WP
- IPhone development swift foundation 09 assets
- Correlation
- [sg function] lightoj Partitioning Game
- Awk getting started to proficient series - awk quick start
- Uboot migration
- Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028
- 常用sql集合
猜你喜欢

常用sql集合

仿网易云音乐小程序

Blue Bridge Cup Guoxin Changtian MCU -- program download (III)

What indicators should be paid attention to in current limit monitoring?

Cesium terrain clipping draw polygon clipping

gslb(global server load balance)技術的一點理解
![[SRS] build a specified version of SRS](/img/01/0d2d762e01b304220b8924d20277e3.jpg)
[SRS] build a specified version of SRS

Station B, dark horse programmer, employee management system, access conflict related (there is an unhandled exception at 0x00007ff633a4c54d (in employee management system.Exe): 0xc0000005: read locat

JS closure knowledge points essence

On my first day at work, this API timeout optimization put me down!
随机推荐
JS Demo calcule combien de jours il reste de l'année
Collection | pytoch common loss function disassembly
Plug - in Oil Monkey
Décompiler et modifier un exe ou une DLL non source en utilisant dnspy
Tidb's initial experience of ticdc6.0
English topic assignment (28)
No more! Technical team members resign collectively
十大券商开户注册安全靠谱吗?有没有风险的?
Conditional statements of shell programming
Global and Chinese market of gallic acid 2022-2028: Research Report on technology, participants, trends, market size and share
JS closure knowledge points essence
Persistence of Nacos
仿网易云音乐小程序
Decompile and modify the non source exe or DLL with dnspy
How PHP adds two numbers
Oil monkey plug-in
Dynamic research and future planning analysis report of China's urban water supply industry Ⓝ 2022 ~ 2028
Analysis report on the development prospect and investment strategy of global and Chinese modular automation systems Ⓟ 2022 ~ 2027
Electronic tube: Literature Research on basic characteristics of 6j1
Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028