classSolution: defcountAnagrams(self, s: str) -> int: MOD = 10 ** 9 + 7 ans = mul = 1 for s in s.split(): cnt = Counter() for i, c inenumerate(s, 1): cnt[c] += 1 mul = mul * cnt[c] % MOD ans = ans * i % MOD return ans * pow(mul, -1, MOD) % MOD
longpow(long x, int n){ long res = 1L; for (; n; n /= 2) { if (n % 2) res = res * x % MOD; x = x * x % MOD; } return res; }
public: intcountAnagrams(string &s){ long ans = 1L, mul = 1L; int cnt[26]{}; for (int i = 0, j = 0; i < s.length(); ++i) { if (s[i] == ' ') { memset(cnt, 0, sizeof(cnt)); j = 0; } else { mul = mul * ++cnt[s[i] - 'a'] % MOD; ans = ans * ++j % MOD; } } return ans * pow(mul, MOD - 2) % MOD; } };
funccountAnagrams(s string)int { ans, mul := 1, 1 for _, s := range strings.Split(s, " ") { cnt := [26]int{} for i, c := range s { cnt[c-'a']++ mul = mul * cnt[c-'a'] % mod ans = ans * (i + 1) % mod } } return ans * pow(mul, mod-2) % mod }
funcpow(x, n int)int { res := 1 for ; n > 0; n >>= 1 { if n&1 > 0 { res = res * x % mod } x = x * x % mod } return res }