✅☑Beats 100% users || [C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n\n\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1 (Maps)\n1. Counting Characters: It iterates through e
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n![Screenshot 2023-12-24 062247.png](https://assets.leetcode.com/users/images/aa9d5037-ae53-41f5-bd39-843095148b19_1703895569.8735316.png)\n\n\n\n# Approaches\n(Also explained in the code)\n\n#### ***Approach 1 (Maps)***\n1. **Counting Characters:** It iterates through each word in the words vector and counts the occurrences of each character, storing the counts in an unordered map counts.\n\n1. **Checking if Characters Can Be Distributed Equally:** Then, it checks if the count of each character in the counts map is divisible by the total number of words n. If any character count isn\'t evenly divisible by n, it returns false. If all characters meet this condition, it returns true.\n\n# Complexity\n- Time complexity:\n   $$O(n).k$$\n    \n\n- Space complexity:\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    bool makeEqual(vector<string>& words) {\n        unordered_map<char, int> counts;\n        for (auto a  : words) {\n            for (char x : a) {\n                counts[x]++;\n            }\n        }\n        \n        int n = words.size();\n        for (auto a : counts) {\n            if (a.second % n != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n};\n\n\n\n```\n```Java []\n\nimport java.util.*;\n\nclass Solution {\n    public boolean makeEqual(String[] words) {\n        Map<Character, Integer> counts = new HashMap<>();\n        \n        for (String word : words) {\n            for (char c : word.toCharArray()) {\n                counts.put(c, counts.getOrDefault(c, 0) + 1);\n            }\n        }\n        \n        int n = words.length;\n        for (Map.Entry<Character, Integer> entry : counts.entrySet()) {\n            if (entry.getValue() % n != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    def makeEqual(self, words: List[str]) -> bool:\n        counts = {}\n        \n        for word in words:\n            for c in word:\n                counts[c] = counts.get(c, 0) + 1\n        \n        n = len(words)\n        for val in counts.values():\n            if val % n != 0:\n                return False\n        \n        return True\n\n\n\n```\n```javascript []\nvar makeEqual = function(words) {\n    let counts = new Map();\n    \n    for (let word of words) {\n        for (let c of word) {\n            counts.set(c, (counts.get(c) || 0) + 1);\n        }\n    }\n    \n    let n = words.length;\n    for (let val of counts.values()) {\n        if (val % n !== 0) {\n            return false;\n        }\n    }\n    \n    return true;\n};\n\n\n\n```\n---\n#### ***Approach 2 (Vector)***\n\n\n1. **Initialization:** Create a count array counts of size 26 (for each letter of the English alphabet) and initialize all counts to 0.\n\n1. **Counting:** Loop through each word in the input vector. For each character in every word, increment its respective count in the `counts` array.\n\n1. **Check distribution:** Determine the number of words (`n`) in the input vector. Then, iterate through the counts array. If any character count is not divisible by the total number of words (`n`), return `false`. This means the character distribution across all words is not equal.\n\n1. If all characters\' counts are divisible by the total number of words, return `true`. This implies that the character distribution can be made equal across all words in the input vector.\n\n# Complexity\n- Time complexity:\n   $$O(n.k)$$\n    \n\n- Space complexity:\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    bool makeEqual(vector<string>& words) {\n        vector<int> counts(26, 0);\n        for (string word : words) {\n            for (char c : word) {\n                counts[c - \'a\']++;\n            }\n        }\n        \n        int n = words.size();\n        for (int val : counts) {\n            if (val % n != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n};\n\n\n\n\n```\n```Java []\nclass Solution {\n    public boolean makeEqual(String[] words) {\n        int[] counts = new int[26];\n        for (String word : words) {\n            for (char c : word.toCharArray()) {\n                counts[c - \'a\']++;\n            }\n        }\n        \n        int n = words.length;\n        for (int val : counts) {\n            if (val % n != 0) {\n                return false;\n            }\n        }\n        \n        return true;\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    def makeEqual(self, words: List[str]) -> bool:\n        counts = [0] * 26\n        for word in words:\n            for c in word:\n                counts[ord(c) - ord(\'a\')] += 1\n        \n        n = len(words)\n        for val in counts:\n            if val % n != 0:\n                return False\n\n        return True\n\n\n```\n```javascript []\nvar makeEqual = function(words) {\n    let counts = new Array(26).fill(0);\n    \n    for (let word of words) {\n        for (let i = 0; i < word.length; i++) {\n            let charCode = word.charCodeAt(i) - \'a\'.charCodeAt(0);\n            counts[charCode]++;\n        }\n    }\n    \n    let n = words.length;\n    for (let val of counts) {\n        if (val % n !== 0) {\n            return false;\n        }\n    }\n    \n    return true;\n};\n\n\n\n```\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Hash Table', 'String', 'Counting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']