Daily Problem | Properly Explained | C++ ✅ | Java ✅ | Python ✅
Intuition\n Describe your first thoughts on how to solve this problem. \nThe objective of this question is to check if, through a sequence of the described oper
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe objective of this question is to check if, through a sequence of the described operations, one can make all the strings in the array words identical. If it\'s possible to achieve this for any combination of strings and operations, the function should return true; otherwise, it should return false.\n\n# Approach - Frequency Counting\n<!-- Describe your approach to solving the problem. -->\n**Pseudocode**\n```\nFunction makeEqual(words):\n    characterCount = array of size 26, initialized to 0\n\n    // Count the frequency of each character in all words\n    for each word in words:\n        for each character in word:\n            characterCount[character - \'a\']++\n\n    // Check if the count of each character is divisible by the total number of words\n    for each count in characterCount:\n        if count % size of words != 0:\n            // If not divisible, characters cannot be rearranged to make all words equal\n            return false\n\n    // If all characters can be rearranged to make words equal, return true\n    return true\n\n```\nThe function `makeEqual` checks if it is possible to rearrange the characters in a given list of words such that each word has the same characters in the same frequency.\n\n**1. Initialize Character Count Array-**\n```\ncharacterCount = array of size 26, initialized to 0\n```\n\nInitializes an array `characterCount` of size 26, where each index corresponds to a lowercase English letter, and the array values represent the count of each character.\n\n**2. Count Character Frequencies-**\n```\n// Count the frequency of each character in all words\nfor each word in words:\n    for each character in word:\n        characterCount[character - \'a\']++\n```\nIterates through each word in the input list of words. For each character in each word, increments the corresponding count in the `characterCount` array. This loop effectively counts the frequency of each character across all words.\n\n**3. Check Divisibility-**\n```\n// Check if the count of each character is divisible by the total number of words\nfor each count in characterCount:\n    if count % size of words != 0:\n        // If not divisible, characters cannot be rearranged to make all words equal\n        return false\n```\nIterates through the `characterCount` array. For each count of a character, checks if it is divisible by the total number of words. If any character\'s count is not divisible evenly by the total number of words, it means characters cannot be rearranged to make all words equal. In such a case, the function returns `false`.\n\n**4. Return Result-**\n```\n// If all characters can be rearranged to make words equal, return true\nreturn true\n```\nIf the loop completes without returning `false`, it means that all characters can be rearranged to make all words have the same characters in the same frequency. In this case, the function returns `true`.\n\n\nLet us see with the help of an example:\nInput: words = ["ab","a"]\n\n1. First we initialize `characterCount` an array of size 26 (for each letter of the alphabet) to zeros.\n2. Next we loop through each word in the input vector `words`:\n- For the first word "ab":\n```\nIncrement the count of \'a\' by 1.\nIncrement the count of \'b\' by 1.\n```\n- For the second word "a":\n```\nIncrement the count of \'a\' by 1.\n\n```\n3. After iteration check if the count of each character is divisible by the total number of words:\n- The count of \'a\' is 2 (1 from the first word + 1 from the second word), and the count of \'b\' is 1.\n- The total number of words is 2.\n- Check if `count % words.size() != 0` for each character count.\n```\nFor \'a\': 2 % 2 == 0 (divisible), so continue.\nFor \'b\': 1 % 2 != 0 (not divisible), so return false.\n```\n4. Finally, the function returns `false` because not all characters can be rearranged to make all words equal.\n\n\n\n\n\n# Complexity\n- Time complexity: O(N * M), \n\nwhere N is the number of words in the input array and M is the average length of the words. This is because the solution iterates through each character in each word to update the character count array.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1) \n\nbecause the size of the characterCount array is constant (26 elements), regardless of the size of the input. The space used is independent of the input size.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# C++ Code\n```\nclass Solution {\npublic:\n    // Function to check if the characters in the words can be rearranged\n    // to make each word have the same characters in the same frequency.\n    bool makeEqual(vector<string>& words) {\n\n        // Array to store the count of each character (initialized to 0).\n        vector<int> characterCount(26, 0);\n\n        // Loop through each word in the input vector \'words\'.\n        for (const string& inputWord : words) {\n            // Loop through each character in the current word.\n            for (char inputChar : inputWord) {\n                // Increment the count of the current character in \'characterCount\'.\n                characterCount[inputChar - \'a\']++;\n            }\n        }\n\n        // Check if the count of each character is divisible by the total number of words.\n        for (int count : characterCount) {\n            if (count % words.size() != 0) {\n                // If not divisible, characters cannot be rearranged to make all words equal.\n                return false;\n            }\n        }\n\n        // If all characters can be rearranged to make words equal, return true.\n        return true;\n    }\n};\n\n```\n\n# JAVA Code\n```\nclass Solution {\n    // Function to check if characters in words can be rearranged\n    // to make each word have the same characters in the same frequency.\n    public boolean makeEqual(String[] words) {\n        \n        // Array to store the count of each character (initialized to 0).\n        final int[] characterCount = new int[26];\n\n        // Loop through each word in the input array \'words\'.\n        for (String inputWord : words) {\n            // Loop through each character in the current word.\n            for (char inputChar : inputWord.toCharArray()) {\n                // Increment the count of the current character in \'characterCount\'.\n                characterCount[inputChar - \'a\']++;\n            }\n        }\n\n        // Check if the count of each character is divisible by the total number of words.\n        for (int count : characterCount) {\n            if (count % words.length != 0) {\n                // If not divisible, characters cannot be rearranged to make all words equal.\n                return false;\n            }\n        }\n\n        // If all characters can be rearranged to make words equal, return true.\n        return true;\n    }\n}\n\n```\n\n# PYTHON Code\n```\nclass Solution:\n    # Function to check if characters in words can be rearranged\n    # to make each word have the same characters in the same frequency.\n    def makeEqual(self, words):\n        # Array to store the count of each character (initialized to 0).\n        characterCount = [0] * 26\n\n        # Loop through each word in the input array \'words\'.\n        for inputWord in words:\n            # Loop through each character in the current word.\n            for inputChar in inputWord:\n                # Increment the count of the current character in \'characterCount\'.\n                characterCount[ord(inputChar) - ord(\'a\')] += 1\n\n        # Check if the count of each character is divisible by the total number of words.\n        for count in characterCount:\n            if count % len(words) != 0:\n                # If not divisible, characters cannot be rearranged to make all words equal.\n                return False\n\n        # If all characters can be rearranged to make words equal, return True.\n        return True\n\n```
['Hash Table', 'String', 'Counting', 'Python', 'C++', 'Java', 'Python3']