{"id":8562,"date":"2021-08-11T12:20:06","date_gmt":"2021-08-11T19:20:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8562"},"modified":"2021-08-11T12:27:00","modified_gmt":"2021-08-11T19:27:00","slug":"leetcode-1897-redistribute-characters-to-make-all-strings-equal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1897-redistribute-characters-to-make-all-strings-equal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1897. Redistribute Characters to Make All Strings Equal"},"content":{"rendered":"\n<p>You are given an array of strings&nbsp;<code>words<\/code>&nbsp;(<strong>0-indexed<\/strong>).<\/p>\n\n\n\n<p>In one operation, pick two&nbsp;<strong>distinct<\/strong>&nbsp;indices&nbsp;<code>i<\/code>&nbsp;and&nbsp;<code>j<\/code>, where&nbsp;<code>words[i]<\/code>&nbsp;is a non-empty string, and move&nbsp;<strong>any<\/strong>&nbsp;character from&nbsp;<code>words[i]<\/code>&nbsp;to&nbsp;<strong>any<\/strong>&nbsp;position in&nbsp;<code>words[j]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if you can make<strong>&nbsp;every<\/strong>&nbsp;string in&nbsp;<\/em><code>words<\/code><em>&nbsp;<strong>equal&nbsp;<\/strong>using&nbsp;<strong>any<\/strong>&nbsp;number of operations<\/em>,<em>&nbsp;and&nbsp;<\/em><code>false<\/code>&nbsp;<em>otherwise<\/em>.<\/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> words = [\"abc\",\"aabc\",\"bc\"]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Move the first 'a' in <code>words[1] to the front of words[2],\nto make <\/code><code>words[1]<\/code> = \"abc\" and words[2] = \"abc\".\nAll the strings are now equal to \"abc\", so return <code>true<\/code>.\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> words = [\"ab\",\"a\"]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> It is impossible to make all the strings equal using the operation.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= words.length &lt;= 100<\/code><\/li><li><code>1 &lt;= words[i].length &lt;= 100<\/code><\/li><li><code>words[i]<\/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>Count the frequency of each character, it must be a multiplier of n such that we can evenly distribute it to all the words.<br>e.g. n = 3, a = 9, b = 6, c = 3, each word will be &#8220;aaabbc&#8221;.<\/p>\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 makeEqual(vector<string>& words) {\n    vector<int> freq(26);\n    for (const auto& word : words)\n      for (char c : word)\n        ++freq[c - 'a'];    \n    for (int f : freq)\n      if (f % words.size()) return false;        \n    return true;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3 one-liner<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def makeEqual(self, words: List[str]) -> bool:\n    return all(c % len(words) == 0 for c in Counter(''.join(words)).values())\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of strings&nbsp;words&nbsp;(0-indexed). In one operation, pick two&nbsp;distinct&nbsp;indices&nbsp;i&nbsp;and&nbsp;j, where&nbsp;words[i]&nbsp;is a non-empty string, and move&nbsp;any&nbsp;character from&nbsp;words[i]&nbsp;to&nbsp;any&nbsp;position in&nbsp;words[j]. Return&nbsp;true&nbsp;if you can make&nbsp;every&nbsp;string in&nbsp;words&nbsp;equal&nbsp;using&nbsp;any&nbsp;number&#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":[8,222,82,4],"class_list":["post-8562","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-counting","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8562","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=8562"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8562\/revisions"}],"predecessor-version":[{"id":8566,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8562\/revisions\/8566"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}