MATHS | C++ | O(N)
Time Complexity: O(N)\n## Theory:\n\nGiven string: "aabbc"\n\n- Total Length: The length of the string is 5 characters.\n  \n- Character Counts:\n  - \'a\' appe
## Time Complexity: O(N)\n## Theory:\n\nGiven string: "aabbc"\n\n- **Total Length**: The length of the string is 5 characters.\n  \n- **Character Counts**:\n  - \'a\' appears twice.\n  - \'b\' appears twice.\n  - \'c\' appears once.\n\nTo find the total number of distinct permutations (anagrams) of the string "aabbc", we use the formula:\n\\[ \\frac{5!}{2! \\times 2! \\times 1!} \\]\n\nHere\'s how this formula works:\n- \\( 5! \\) (5 factorial) represents the total number of permutations if all characters were unique.\n- \\( 2! \\) represents the factorial of the count of \'a\', dividing by this accounts for the fact that \'a\' characters are indistinguishable.\n- \\( 2! \\) represents the factorial of the count of \'b\', dividing by this accounts for the fact that \'b\' characters are indistinguishable.\n- \\( 1! \\) represents the factorial of the count of \'c\', although in this case it is not technically necessary since 1!\n\n# Code\n```\nclass Solution {\npublic:\n    int countAnagrams(string s) {\n        const int MOD = 1000000007;\n        int i = 0;\n        long long NUM = 1;\n        long long DEN = 1;\n        \n        unordered_map<char, int> hash;\n        int offset = 0;\n        \n        while (i < s.length()) {\n            if (s[i] == \' \') {\n                hash.clear();\n                offset = i + 1;\n                i++;\n                continue;\n            }\n            \n            hash[s[i]]++;\n            NUM = NUM * (i + 1 - offset) % MOD;\n            DEN = DEN * hash[s[i]] % MOD;\n            \n            i++;\n        }\n\n        long long inverse_DEN = modInverse(DEN, MOD);\n\n        int result = (NUM * inverse_DEN) % MOD;\n\n        return result;\n    }\n\nprivate:\n    long long modInverse(long long a, long long m) {\n        long long m0 = m;\n        long long y = 0, x = 1;\n\n        if (m == 1) return 0;\n\n        while (a > 1) {\n            long long q = a / m;\n            long long t = m;\n\n            m = a % m, a = t;\n            t = y;\n\n            y = x - q * y;\n            x = t;\n        }\n\n        if (x < 0) x += m0;\n        return x;\n    }\n};\n\n```
['C++']