{"id":7619,"date":"2020-11-07T22:06:06","date_gmt":"2020-11-08T06:06:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7619"},"modified":"2020-11-07T22:10:58","modified_gmt":"2020-11-08T06:10:58","slug":"leetcode-1647-minimum-deletions-to-make-character-frequencies-unique","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1647-minimum-deletions-to-make-character-frequencies-unique\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1647. Minimum Deletions to Make Character Frequencies Unique"},"content":{"rendered":"\n<p>A string&nbsp;<code>s<\/code>&nbsp;is called&nbsp;<strong>good<\/strong>&nbsp;if there are no two different characters in&nbsp;<code>s<\/code>&nbsp;that have the same&nbsp;<strong>frequency<\/strong>.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>, return<em>&nbsp;the&nbsp;<strong>minimum<\/strong>&nbsp;number of characters you need to delete to make&nbsp;<\/em><code>s<\/code><em>&nbsp;<strong>good<\/strong>.<\/em><\/p>\n\n\n\n<p>The&nbsp;<strong>frequency<\/strong>&nbsp;of a character in a string is the number of times it appears in the string. For example, in the string&nbsp;<code>\"aab\"<\/code>, the&nbsp;<strong>frequency<\/strong>&nbsp;of&nbsp;<code>'a'<\/code>&nbsp;is&nbsp;<code>2<\/code>, while the&nbsp;<strong>frequency<\/strong>&nbsp;of&nbsp;<code>'b'<\/code>&nbsp;is&nbsp;<code>1<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"aab\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> <code>s<\/code> is already good.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"aaabbbcc\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> You can delete two 'b's resulting in the good string \"aaabcc\".\nAnother way it to delete one 'b' and one 'c' resulting in the good string \"aaabbc\".<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"ceabaacb\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> You can delete both 'c's resulting in the good string \"eabaab\".\nNote that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;contains only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>The deletion order doesn&#8217;t matter, we can process from &#8216;a&#8217; to &#8216;z&#8217;. Use a hashtable to store the &#8220;final frequency&#8221; so far, for each char, decrease its frequency until it becomes unique in the final frequency hashtable.<\/p>\n\n\n\n<p>Time complexity: O(n + 26^2)<br>Space complexity: O(26)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int minDeletions(string s) {\n    vector<int> freq(26);\n    for (char c : s) ++freq[c - 'a'];    \n    unordered_set<int> seen;\n    int ans = 0;\n    for (int f : freq) {\n      while (f && !seen.insert(f).second) {        \n        --f;\n        ++ans;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A string&nbsp;s&nbsp;is called&nbsp;good&nbsp;if there are no two different characters in&nbsp;s&nbsp;that have the same&nbsp;frequency. Given a string&nbsp;s, return&nbsp;the&nbsp;minimum&nbsp;number of characters you need to delete to make&nbsp;s&nbsp;good.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[24,82,177,4],"class_list":["post-7619","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-frequency","tag-hashtable","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7619"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7619\/revisions"}],"predecessor-version":[{"id":7621,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7619\/revisions\/7621"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}