当前位置:网站首页>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).
边栏推荐
- China's Call Center Industry 14th five year plan direction and operation analysis report Ⓔ 2022 ~ 2028
- Why use pycharm to run the use case successfully but cannot exit?
- gslb(global server load balance)技术的一点理解
- 90 后,辞职创业,说要卷死云数据库
- 6.0 kernel driver character driver
- Investment planning analysis and prospect prediction report of China's satellite application industry during the 14th five year plan Ⓑ 2022 ~ 2028
- The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
- Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
- China HDI market production and marketing demand and investment forecast analysis report Ⓢ 2022 ~ 2028
- Data consistency between redis and database
猜你喜欢

What should the future of the Internet be like when Silicon Valley employees flee the big factory and rush to Web3| Footprint Analytics

2022 safety officer-a certificate registration examination and summary of safety officer-a certificate examination

Asynchronous artifact: implementation principle and usage scenario of completable future

TiDB 之 TiCDC6.0 初体验
![[SRS] build a specified version of SRS](/img/01/0d2d762e01b304220b8924d20277e3.jpg)
[SRS] build a specified version of SRS

Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)

Collections SQL communes

Introduction to kubernetes

treevalue——Master Nested Data Like Tensor

treevalue——Master Nested Data Like Tensor
随机推荐
Global and Chinese market of gallic acid 2022-2028: Research Report on technology, participants, trends, market size and share
Common SQL sets
The 14th five year plan for the construction of Chinese Enterprise Universities and the feasibility study report on investment Ⓓ 2022 ~ 2028
Data consistency between redis and database
Leetcode problem solving - 235 Nearest common ancestor of binary search tree
2022 safety officer-a certificate registration examination and summary of safety officer-a certificate examination
[sg function]split game (2020 Jiangxi university student programming competition)
Are the top ten securities companies safe to open accounts and register? Is there any risk?
Report on the current situation and development trend of ethoxylated sodium alkyl sulfate industry in the world and China Ⓞ 2022 ~ 2027
China's TPMS industry demand forecast and future development trend analysis report Ⓐ 2022 ~ 2028
On my first day at work, this API timeout optimization put me down!
How to install sentinel console
TiDB 之 TiCDC6.0 初体验
使用dnSpy對無源碼EXE或DLL進行反編譯並且修改
QGIS grid processing DEM data reclassification
JS demo calculate how many days are left in this year
内存分析器 (MAT)
An expression that regularly matches one of two strings
Morning flowers and evening flowers
Global and Chinese market of recycled yarn 2022-2028: Research Report on technology, participants, trends, market size and share