当前位置:网站首页>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 <= 105
s
Small 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).
边栏推荐
- Yiwen teaches you how to choose your own NFT trading market
- [sg function]split game (2020 Jiangxi university student programming competition)
- China's coal industry investment strategic planning future production and marketing demand forecast report Ⓘ 2022 ~ 2028
- Cognitive fallacy: what is dimensional curse
- Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
- JS notes (III)
- China HDI market production and marketing demand and investment forecast analysis report Ⓢ 2022 ~ 2028
- DOM light switch case
- pivot ROP Emporium
- Is the account opening of Guotai Junan Securities safe and reliable? How to open Guotai Junan Securities Account
猜你喜欢
What indicators should be paid attention to in current limit monitoring?
The latest analysis of crane driver (limited to bridge crane) in 2022 and the test questions and analysis of crane driver (limited to bridge crane)
treevalue——Master Nested Data Like Tensor
On my first day at work, this API timeout optimization put me down!
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
gslb(global server load balance)技術的一點理解
Collections SQL communes
Preliminary analysis of smart microwave radar module
2022 safety officer-a certificate registration examination and summary of safety officer-a certificate examination
随机推荐
How to store null value on the disk of yyds dry inventory?
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
On my first day at work, this API timeout optimization put me down!
Implementation principle of inheritance, encapsulation and polymorphism
Rest reference
[actual combat record] record the whole process of the server being attacked (redis vulnerability)
The latest analysis of crane driver (limited to bridge crane) in 2022 and the test questions and analysis of crane driver (limited to bridge crane)
Global and Chinese market of wireless hard disk 2022-2028: Research Report on technology, participants, trends, market size and share
A little understanding of GSLB (global server load balance) technology
Development trend and market demand analysis report of China's energy storage battery industry Ⓩ 2022 ~ 2028
Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
What is the content of the securities practice examination?
Under the double reduction policy, research travel may become a big winner
Cognitive fallacy: what is dimensional curse
Leetcode problem solving - 235 Nearest common ancestor of binary search tree
China's TPMS industry demand forecast and future development trend analysis report Ⓐ 2022 ~ 2028
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
How to obtain opensea data through opensea JS
Correlation
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers