# 1647. Minimum Deletions to Make Character Frequencies Unique ###### tags: `Leetcode` `Medium` `Microsoft` Link:https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/ ## 思路 计数+Hashset 原本的想法是 计数之后排序 这样找到重复的了之后,就找比它小的的空缺即可 但其实仔细想想其实不需要排序 只要在insert进hashset的时候保证set里面没有重复的数字,最终得到的结果就是答案 ## Code ```java= class Solution { public int minDeletions(String s) { int[] freq = new int[26]; int ans = 0; for(int i = 0;i < s.length();i++){ freq[(int)(s.charAt(i)-97)]++; } Set<Integer> set = new HashSet<>(); for(int i = 0; i < freq.length;i++){ if(!set.contains(freq[i]) && freq[i]!=0){ set.add(freq[i]); } else{ while(freq[i]!=0 && set.contains(freq[i])){ freq[i]--; ans++; } if(freq[i]!=0) set.add(freq[i]); } } return ans; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up