Two HashMaps
Intuition\nTwo HashMaps - for the frequencies of letters in words and a common one for inverted MODs for the frequencies of letters in words (memo).\nAll multip
# Intuition\nTwo HashMaps - for the frequencies of letters in words and a common one for inverted MODs for the frequencies of letters in words (memo).\nAll multiplications and "divisions" in LONG, finally .toInt()\n# Approach\nTwo HashMaps - for the frequencies of letters in words and a common one for inverted MODs for the frequencies of letters in words (memo).\nAll multiplications and "divisions" in LONG, finally .toInt()\n# Complexity\n- Time complexity:\nO(n*log(n))\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\n    fun countAnagrams(s: String): Int {\n        var prev = \'*\'\n        var map = HashMap<Char, Int>()\n        val INV_fact = HashMap<Int, Int>()\n        INV_fact[1] = 1\n        var rez = 1L\n        var count = 0\n        val mod = 1_000_000_007\n\n        fun binPow (a: Long, b: Int): Long {\n            var res = 1L\n            var base = a\n            var pow = b\n            while (pow > 0) {\n                if ((pow and 1) == 1) res = (res * base) % mod\n                pow = pow shr 1\n                base = (base * base) % mod\n            }\n            return res\n        }\n        fun inverseElement(x: Long): Long {\n            return binPow(x, mod - 2)\n        }\n        for (i in 0 until s.length) {\n            val ch = s[i]\n            if (!(ch==\' \' || i==s.length-1)) {\n                map[ch] = map.getOrDefault(ch, 0) + 1\n                count++\n            } \n            else {\n                if (i==s.length-1) {\n                    map[ch] = map.getOrDefault(ch, 0) + 1\n                    count++\n                }\n                while (count>1) {\n                    rez = (rez * count)%mod\n                    count--\n                }\n                count = 0\n// divide to value!                   \n                for((key, value) in map){\n                    if (INV_fact.containsKey(value)) {\n                        rez = (rez * INV_fact.getValue(value))%mod\n                    }\n                    else {\n                        var fact = 1L\n                        for (j in 2..value) {\n                            fact = (fact * j) % mod\n                        }\n                        val INV = inverseElement(fact).toInt()\n                        INV_fact.put(value,INV) \n                        rez = (rez * INV)%mod\n                    }\n                }\n                map = HashMap<Char, Int>()\n            }\n        }\n        return rez.toInt()\n    }\n}\n```
['Kotlin']