当前位置:网站首页>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).
边栏推荐
- DR882-Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-Wi-Fi-Module-5G-high-power
- Idea shortcut word operation
- Is it safe and reliable to open an account and register for stock speculation? Is there any risk?
- Tkinter Huarong Road 4x4 tutorial III
- Decompile and modify the non source exe or DLL with dnspy
- No more! Technical team members resign collectively
- Report on the current situation and development trend of ethoxylated sodium alkyl sulfate industry in the world and China Ⓞ 2022 ~ 2027
- Imitation Netease cloud music applet
- Base ring tree Cartesian tree
- Under the double reduction policy, research travel may become a big winner
猜你喜欢

On my first day at work, this API timeout optimization put me down!

Decompile and modify the non source exe or DLL with dnspy

Redis single thread and multi thread

Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files

On my first day at work, this API timeout optimization put me down!

使用dnSpy對無源碼EXE或DLL進行反編譯並且修改

Décompiler et modifier un exe ou une DLL non source en utilisant dnspy

UC Berkeley proposes a multitask framework slip

TiDB 之 TiCDC6.0 初体验

使用dnSpy对无源码EXE或DLL进行反编译并且修改
随机推荐
[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]
Development trend and market demand analysis report of China's energy storage battery industry Ⓩ 2022 ~ 2028
内存分析器 (MAT)
Code in keil5 -- use the code formatting tool astyle (plug-in)
JS closure knowledge points essence
China's TPMS industry demand forecast and future development trend analysis report Ⓐ 2022 ~ 2028
How PHP adds two numbers
Sed、Awk
What indicators should be paid attention to in current limit monitoring?
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
On my first day at work, this API timeout optimization put me down!
An expression that regularly matches one of two strings
Team collaborative combat penetration tool CS artifact cobalt strike
Farmersworld farmers world, no faith, how to talk about success?
油猴插件
regular expression
Minio deployment
Kali2021.4a build PWN environment
股票炒股开户注册安全靠谱吗?有没有风险的?
pivot ROP Emporium