{"id":9305,"date":"2021-12-31T05:27:22","date_gmt":"2021-12-31T13:27:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9305"},"modified":"2021-12-31T05:27:39","modified_gmt":"2021-12-31T13:27:39","slug":"leetcode-1941-check-if-all-characters-have-equal-number-of-occurrences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1941-check-if-all-characters-have-equal-number-of-occurrences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1941. Check if All Characters Have Equal Number of Occurrences"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>, return&nbsp;<code>true<\/code><em>&nbsp;if&nbsp;<\/em><code>s<\/code><em>&nbsp;is a&nbsp;<strong>good<\/strong>&nbsp;string, or&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise<\/em>.<\/p>\n\n\n\n<p>A string&nbsp;<code>s<\/code>&nbsp;is&nbsp;<strong>good<\/strong>&nbsp;if&nbsp;<strong>all<\/strong>&nbsp;the characters that appear in&nbsp;<code>s<\/code>&nbsp;have the&nbsp;<strong>same<\/strong>&nbsp;number of occurrences (i.e., the same frequency).<\/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 = \"abacbc\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The characters that appear in s are 'a', 'b', and 'c'. All characters occur 2 times in s.\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 = \"aaabb\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> The characters that appear in s are 'a' and 'b'.\n'a' occurs 3 times while 'b' occurs 2 times, which is not the same number of times.\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;= 1000<\/code><\/li><li><code>s<\/code>&nbsp;consists of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool areOccurrencesEqual(string s) {\n    vector<int> m(26);\n    int maxCount = 0;\n    for (char c : s)\n      maxCount = max(maxCount, ++m[c - 'a']);\n    return all_of(begin(m), end(m), [maxCount](int c) -> bool {\n      return c == 0 || c == maxCount;\n    });\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def areOccurrencesEqual(self, s: str) -> bool:\n    return len(set(Counter(s).values())) == 1\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, return&nbsp;true&nbsp;if&nbsp;s&nbsp;is a&nbsp;good&nbsp;string, or&nbsp;false&nbsp;otherwise. A string&nbsp;s&nbsp;is&nbsp;good&nbsp;if&nbsp;all&nbsp;the characters that appear in&nbsp;s&nbsp;have the&nbsp;same&nbsp;number of occurrences (i.e., the same frequency). Example 1: Input: s = &#8220;abacbc&#8221;&#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":[222,82,4],"class_list":["post-9305","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9305","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=9305"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9305\/revisions"}],"predecessor-version":[{"id":9307,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9305\/revisions\/9307"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}