Simple C++ Solution
Intuition\n\nThe problem is asking for the number of distinct anagrams of a given string where each word in the string is permuted independently. So by using th
# Intuition\n\nThe problem is asking for the number of distinct anagrams of a given string where each word in the string is permuted independently. So by using the simple formula we use in Aptitude Questions i.e No of different permutations of a word with some repeating characters is:\n(No. of characters)! / (repating character frequency)!.\n\n# Approach\nThe approach is to split the string into words and then calculate the number of permutations for each word. For each word, we calculate the factorial of the length of the word and divide it by the factorial of the frequency of each character in the word. This gives us the number of distinct permutations of the word. We then multiply the number of permutations for each word together to get the total number of anagrams of the string.\nTo handle large numbers and prevent overflow, we perform all calculations modulo 10^9+7\nWhen dividing, we calculate the modulo inverse of the denominator using Fermat\u2019s Little Theorem.\n\n# Complexity\n- Time complexity: $$O(n)$$ \n\n- Space complexity: $$O(n)$$ \n\n# Code\n```\n#define MOD 1000000007\nclass Solution {\npublic:\n    long long power(long long x, long long y) {\n        long long res = 1;\n        x = x % MOD;\n        while (y > 0) {\n            if (y & 1)\n                res = (res * x) % MOD;\n            y = y >> 1;\n            x = (x * x) % MOD;\n        }\n        return res;\n    }\n    long long factorial(int N) {\n        long long f = 1;\n        for (int i = 2; i <= N; i++)\n            f = (f * i) % MOD;\n\n        return f;\n    }\n    long long wordVariety(string& str) {\n        int n = str.size();\n        long long ans = factorial(n);\n        unordered_map<char, int> m;\n        for (int i = 0; i < n; i++) {\n            m[str[i]]++;\n        }\n        long long res = 1;\n        for (auto i : m) {\n            if (i.second > 1) {\n                res = (res * factorial(i.second)) % MOD;\n            }\n        }\n        ans = (ans * power(res, MOD - 2)) % MOD;\n        return ans%MOD;\n    }\n    long long permutations(vector<string>& s) {\n        int n = s.size();\n        long long ans = 1;\n        for (int i = 0; i < n; i++) {\n            ans = (ans * wordVariety(s[i])) % MOD;\n        }\n        return ans%MOD;\n    }\n    int countAnagrams(string s) {\n        vector<string> parts;\n        string word = "";\n        for (auto i : s) {\n            if (i == \' \') {\n                parts.push_back(word);\n                word = "";\n            } else {\n                word += i;\n            }\n        }\n        parts.push_back(word);\n        return permutations(parts);\n    }\n};\n```
['C++']