Proper Explanation, example dry run 🔥✔ | C++✔ | Java✔|Python✔|
If you find it helpful , please upvote it \uD83D\uDE22\n\n---\n\n\n# Intuition\nBasically the questions asks to check if we can make all the strings equal.\nWha
# If you find it helpful , please upvote it \uD83D\uDE22\n\n---\n\n\n# Intuition\nBasically the questions asks to check if we can make all the strings equal.\nWhat we understand from question ?\n1. to make all the strings equal , we need same characters in all strings\n2. We need same frequency of all the characters in all strings \n\n\n# Approach - Using Map\n```\n1. Declare Map;\n2. Traverse through the strings and add increase count of every char meet in map\n3. Now traverse the map and for every char check whether frequency of the char can be divided equally into all the strings given.\n```\n# Example understanding\nLet\'s take an example given in the problem \n\n`Input: words = ["abc","aabc","bc"]`\n\nstep 1 - create a map\nstep 2 - Traverse through the strings in `words`\n```\nAfter traversing string \'abc\' \n    map\n        a - 1\n        b - 1\n        c - 1\n```\n```\nAfter traversing string \'abc\', \'aabc\'\n    map\n        a - 3\n        b - 2\n        c - 2\nExplanation - previously 1-a and now 2-a so count(a) = 3 , same way others also\n```\n```\nAfter traversing string \'abc\' , \'aabc\' , \'bc\'\n    map\n        a - 3\n        b - 3\n        c - 3\n```\n`step 3` - Now traverse the map and for every char check whether frequency of the char can be divided equally into all the strings given.\n```\nwe know length(words) = 3\nso \n    a % 3 = 0\n    b % 3 = 0\n    c % 3 = 0\n```\n\n# Complexity\n- Time complexity:`O(n^2)` , as we have to traverse each string (one loop) and each char in that string(2nd loop)\n\n- Space complexity:`O(n)` , space required for map\n\n# C++ Code\n```\nclass Solution {\npublic:\n    bool makeEqual(vector<string>& words) {\n        unordered_map<char,int> map;\n        for(auto it:words){\n            for(auto i:it){\n                map[i]++;\n            }\n        }\n        int n = words.size();\n        for(auto it:map){\n            if(it.second % n== 0)continue;\n            else return false;\n        }\n        return true;\n    }\n};\n```\n# Java Code\n```\nclass Solution {\n    public boolean makeEqual(String[] words) {\n        Map<Character, Integer> charCount = new HashMap<>();\n\n        // Count the occurrences of each character\n        for (String word : words) {\n            for (char c : word.toCharArray()) {\n                charCount.put(c, charCount.getOrDefault(c, 0) + 1);\n            }\n        }\n\n        int n = words.length;\n\n        // Check if each character\'s count is a multiple of the number of strings\n        for (Map.Entry<Character, Integer> entry : charCount.entrySet()) {\n            if (entry.getValue() % n != 0) {\n                return false;\n            }\n        }\n\n        return true;\n    }\n\n}\n```\n# Python\n```\nclass Solution:\n    def makeEqual(self, words):\n        char_count = defaultdict(int)\n\n        # Count the occurrences of each character\n        for word in words:\n            for char in word:\n                char_count[char] += 1\n\n        n = len(words)\n\n        # Check if each character\'s count is a multiple of the number of strings\n        for count in char_count.values():\n            if count % n != 0:\n                return False\n\n        return True\n```\n\n---\n\n\n\n
['Python', 'C++', 'Java', 'Python3']