✅Beats 100% - Explained with [ Video ] - Hash Table - Interview Solution
\n\n# YouTube Video Explanation:\n\nhttps://youtu.be/mLlal5Ujrl0\n **If you want a video for this question please write in the comments** \n\n\uD83D\uDD25 Pleas
![Screenshot 2023-12-30 081353.png](https://assets.leetcode.com/users/images/14d7f3e9-9d56-41ca-abdd-5f574f08d458_1703904415.994174.png)\n\n# YouTube Video Explanation:\n\n[https://youtu.be/mLlal5Ujrl0](https://youtu.be/mLlal5Ujrl0)\n<!-- **If you want a video for this question please write in the comments** -->\n\n**\uD83D\uDD25 Please like, share, and subscribe to support our channel\'s mission of making complex concepts easy to understand.**\n\nSubscribe Link: https://www.youtube.com/@leetlogics/?sub_confirmation=1\n\n*Subscribe Goal: 1100 Subscribers*\n*Current Subscribers: 1040*\n\n---\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking whether it is possible to make all strings in the given array equal by performing specific operations. The operations allow moving characters from one string to another.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- First, we check if the length of the array is 1. If it is, all strings are already equal, so we return true.\n- Calculate the total character count across all strings.\n- If the total character count is not divisible by the number of strings, it is impossible to make them equal, so return false.\n- Initialize an array `myMap` to count the occurrences of each character.\n- Traverse all strings and update the character count in `myMap`.\n- Check if each character count is divisible evenly by the number of strings. If any character count is not divisible, return false.\n- If all checks pass, return true.\n\n# Complexity\n- Time complexity: The time complexity is O(N * M), where N is the number of strings and M is the average length of the strings. This is because we traverse each character in each string.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: The space complexity is O(1) since the character count array has a constant size of 26 (assuming lowercase English letters).\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public boolean makeEqual(String[] words) {\n      if (words.length == 1) {\n\t\t\treturn true;\n\t\t}\n\t\tint totalCharCount = 0;\n\t\tfor (String s : words) {\n\t\t\ttotalCharCount += s.length();\n\t\t}\n\t\tif (totalCharCount % words.length != 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\tint[] myMap = new int[26];\n\t\tfor (String s : words) {\n\t\t\tfor (char c : s.toCharArray()) {\n\t\t\t\tmyMap[c - \'a\']++;\n\t\t\t}\n\t\t}\n\t\tfor (int i : myMap) {\n\t\t\tif (i % words.length != 0) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\treturn true; \n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    bool makeEqual(vector<string>& words) {\n        if (words.size() == 1) {\n            return true;\n        }\n\n        int totalCharCount = 0;\n        for (const string& s : words) {\n            totalCharCount += s.length();\n        }\n\n        if (totalCharCount % words.size() != 0) {\n            return false;\n        }\n\n        vector<int> myMap(26, 0);\n        for (const string& s : words) {\n            for (char c : s) {\n                myMap[c - \'a\']++;\n            }\n        }\n\n        for (int i : myMap) {\n            if (i % words.size() != 0) {\n                return false;\n            }\n        }\n\n        return true;\n    }\n};\n```\n```Python []\nclass Solution(object):\n    def makeEqual(self, words):\n        if len(words) == 1:\n            return True\n\n        total_char_count = sum(len(s) for s in words)\n\n        if total_char_count % len(words) != 0:\n            return False\n\n        my_map = [0] * 26\n        for s in words:\n            for c in s:\n                my_map[ord(c) - ord(\'a\')] += 1\n\n        for i in my_map:\n            if i % len(words) != 0:\n                return False\n\n        return True\n        \n```\n```JavaScript []\n/**\n * @param {string[]} words\n * @return {boolean}\n */\nvar makeEqual = function(words) {\n    if (words.length === 1) {\n        return true;\n    }\n\n    let totalCharCount = 0;\n    for (const s of words) {\n        totalCharCount += s.length;\n    }\n\n    if (totalCharCount % words.length !== 0) {\n        return false;\n    }\n\n    let myMap = new Array(26).fill(0);\n    for (const s of words) {\n        for (const c of s) {\n            myMap[c.charCodeAt(0) - \'a\'.charCodeAt(0)]++;\n        }\n    }\n\n    for (const i of myMap) {\n        if (i % words.length !== 0) {\n            return false;\n        }\n    }\n\n    return true;\n};\n```\n![upvote.png](https://assets.leetcode.com/users/images/185525bf-4609-4109-95b6-550eba883ad9_1703904436.6980712.png)\n
['Hash Table', 'String', 'Counting', 'Python', 'C++', 'Java', 'JavaScript']