JAVA | Modulo Multiplicative Inverse | Neat and Clean Solution
Intuition\nFor each string (space seprated) find the count of unique permutation and multiply with the result.\n Describe your first thoughts on how to solve th
# Intuition\nFor each string (space seprated) find the count of unique permutation and multiply with the result.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nHow to get unique permutation - \neg - string = "ababcb"\nunique permuation - 6! / (2! * 3! * 1!).\n\nAs max length of string is 1e5. So we can precomute the factorial from 1 to 1e5.\n\nNote :- when dealing with modulus in division. Use modulo multiplicative inverse technique.\ni.e.  **a / b mod n = (a * pow(b, n -  2)) mod n**  \n    \n\n# Complexity\n- Time complexity: O(1e5 + n * log (1e9))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1e5)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n# Code\n```\nclass Solution {\n    public int countAnagrams(String str) {\n        \n        Map<Integer,Long> map = new HashMap<>();\n        int mod = 1000000000 + 7;\n        \n        long pro = 1;\n        long ans = 1;\n        \n        for(int i = 1 ; i <= 100000 ; i++) {\n            \n            pro = (pro * i) % mod;\n            \n            map.put(i, pro);\n        }\n        \n        for(String s : str.split(" ")) {\n            \n            long num = map.get(s.length());\n            long den = 1;\n\n            int[] freq = new int[26];\n            \n            for(char ch : s.toCharArray()) freq[ch - \'a\']++;\n            \n            for(int ele : freq) {\n                \n                if(ele != 0)\n                \n                den = (den * map.get(ele)) % mod;\n            }   \n            \n            long curr = (num * pow(den, mod - 2)) % mod;\n            ans = (ans * curr) % mod;\n        }\n        \n        return (int)ans;\n    }\n    \n    long pow(long a,long n) {\n        \n        int mod = 1000000000 + 7;\n        \n        long res = 1;\n        \n        while(n > 0) {\n            \n            if(n % 2 == 1) {\n                \n                res = (res * a) % mod;\n                n--;\n            }\n            else {\n                \n                a = (a * a) % mod;\n                n = n / 2;\n            }\n        }\n        \n        return res;\n    }\n}\n```
['Java']