C++ Solution✅ | Detailed Explanation ⭐️
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problems essentially asks us to find the number of distinct permutations for the en
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problems essentially asks us to find the number of distinct permutations for the entire string. \n\nThis boils down to finding the number of distinct permuations of each word in the string and multiplying them to get total number of permuations. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor every word, we get it\'s length. We also keep track of the occurrence of every character in that word. \n\nAfter this let\'s find the number of distinct permuations for this word.\nIt\'s going to be $$({n!}/{(x! * y! * ... z!)})$$, here n is the length of the word, and x, y, z are characters that have repeated x, y, z times.\n\nNow, we just multiply this with our answer and reset all the variables and keep doing this for all the words. \n\nFor calculating the factorials we can precompute all factorials till 1e5.\n\nSince, there is division taking place under modulo, we need to find the modulo multiplicative inverse of $${(x! * y! * ... z!)}$$ with respect to our modulo (1e9+7).\n\nModulo Multiplicative Inverse for a number N, when the modulo is prime is given by : $$(N^{MOD-2})$$ with respect to modulo $${MOD}$$\n\nWe can use binary exponentiation to calculate $$(a^b)$$ in $$log(b)$$ time.\nAlso, keep in mind to use modular multiplication everywhere.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(nlogn)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n# Code\n```\nclass Solution {\nprivate:\n    const int mod=1e9+7;\n    long long fact[100001];\n    \n    int modMul(int a,int b,int mod){\n        return ((long long)(a%mod)*(b%mod))%mod;\n    }\n    \n    int binpow(int a,int b,int mod){\n        if(b==0)return 1;\n        long long res=binpow(a,b/2,mod);\n        res=modMul(res,res,mod);\n        if(b & 1){\n            res=modMul(res,a,mod);\n        }\n        return res;\n    }\n    \n    void computeFactorial(){\n        fact[0]=1;\n        for(int i=1;i<100001;i++){\n            fact[i]=modMul(fact[i-1],i,mod);\n        }\n    }    \n    \n    int modInv(int a,int mod){\n        return binpow(a,mod-2,mod);\n    }\npublic:\n    //  For a given word of len(n) the number of permutations are: \n    //      n!/(x1!*x2!*..xk!),\n    //  where x1,x2,.. are repeating letters.\n    int countAnagrams(string s) {\n       computeFactorial();\n        \n       map<char,int>characterOccurrence;\n       string cur="";\n       int curLen=0;\n       int rep=0;\n       int ans=1;\n       int n=(int)s.size(); \n        \n       for(int i=0;i<n;i++){\n           if(s[i]==\' \'){\n              int currentLenFact=fact[curLen]; \n              int repFact=1;\n              for(auto character:characterOccurrence){\n                 repFact=modMul(repFact,fact[character.second],mod); \n              } \n              ans=modMul(ans,modMul(currentLenFact,modInv(repFact,mod),mod),mod);\n              curLen=0,rep=0;\n              characterOccurrence.clear();\n           }\n           else{\n               characterOccurrence[s[i]]++;\n               curLen++;\n           }\n       }\n       int currentLenFact=fact[curLen];\n       int repFact=1;\n       for(auto character:characterOccurrence){\n          repFact=modMul(repFact,fact[character.second],mod); \n       }\n       ans=modMul(ans,modMul(currentLenFact,modInv(repFact,mod),mod),mod);\n       return ans;\n    }\n};\n```
['C++']