Each word in the string can be rearranged independently. The number of anagrams for a word is the multinomial coefficient: total letters factorial divided by the product of factorials of each letter’s frequency. The answer is the product of anagram counts for all words, modulo 10^9+7.
classSolution {
public:int countAnagrams(string s) {
constint MOD =1e9+7, N =100005;
vector<longlong> fact(N), inv(N);
fact[0] =1;
for (int i =1; i < N; ++i) fact[i] = fact[i-1] * i % MOD;
inv[N-1] = powmod(fact[N-1], MOD-2, MOD);
for (int i = N-2; i >=0; --i) inv[i] = inv[i+1] * (i+1) % MOD;
auto split = [](string& s) {
vector<string> res;
string t;
for (char c : s) {
if (c ==' ') {
if (!t.empty()) res.push_back(t);
t.clear();
} else t += c;
}
if (!t.empty()) res.push_back(t);
return res;
};
auto words = split(s);
longlong ans =1;
for (auto& w : words) {
vector<int> cnt(26);
for (char c : w) cnt[c-'a']++;
longlong cur = fact[w.size()];
for (int x : cnt) cur = cur * inv[x] % MOD;
ans = ans * cur % MOD;
}
return ans;
}
longlongpowmod(longlong a, longlong b, longlong m) {
longlong res =1;
while (b) {
if (b &1) res = res * a % m;
a = a * a % m;
b >>=1;
}
return res;
}
};
classSolution {
publicintcountAnagrams(String s) {
finalint MOD = 1_000_000_007, N = 100005;
long[] fact =newlong[N], inv =newlong[N];
fact[0]= 1;
for (int i = 1; i < N; ++i) fact[i]= fact[i-1]* i % MOD;
inv[N-1]= powmod(fact[N-1], MOD-2, MOD);
for (int i = N-2; i >= 0; --i) inv[i]= inv[i+1]* (i+1) % MOD;
String[] words = s.split(" ");
long ans = 1;
for (String w : words) {
int[] cnt =newint[26];
for (char c : w.toCharArray()) cnt[c-'a']++;
long cur = fact[w.length()];
for (int x : cnt) cur = cur * inv[x]% MOD;
ans = ans * cur % MOD;
}
return (int)ans;
}
longpowmod(long a, long b, long m) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1) res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
}
classSolution {
funcountAnagrams(s: String): Int {
val MOD = 1_000_000_007
val N = 100005val fact = LongArray(N)
val inv = LongArray(N)
fact[0] = 1Lfor (i in1 until N) fact[i] = fact[i-1] * i % MOD
inv[N-1] = powmod(fact[N-1], (MOD-2).toLong(), MOD.toLong())
for (i in N-2 downTo 0) inv[i] = inv[i+1] * (i+1) % MOD
val words = s.split(" ")
var ans = 1Lfor (w in words) {
val cnt = IntArray(26)
for (c in w) cnt[c-'a']++var cur = fact[w.length]
for (x in cnt) cur = cur * inv[x] % MOD
ans = ans * cur % MOD
}
return ans.toInt()
}
funpowmod(a: Long, b: Long, m: Long): Long {
var a = a; var b = b; var res = 1Lwhile (b > 0) {
if (b and 1L==1L) res = res * a % m
a = a * a % m
b = b shr 1 }
return res
}
}
classSolution:
defcountAnagrams(self, s: str) -> int:
MOD =10**9+7 N =100005 fact = [1] * N
inv = [1] * N
for i in range(1, N):
fact[i] = fact[i-1] * i % MOD
inv[N-1] = pow(fact[N-1], MOD-2, MOD)
for i in range(N-2, -1, -1):
inv[i] = inv[i+1] * (i+1) % MOD
ans =1for w in s.split():
cnt = [0] *26for c in w:
cnt[ord(c)-ord('a')] +=1 cur = fact[len(w)]
for x in cnt:
cur = cur * inv[x] % MOD
ans = ans * cur % MOD
return ans
impl Solution {
pubfncount_anagrams(s: String) -> i32 {
constMOD: i64=1_000_000_007;
const N: usize=100005;
letmut fact =vec![1i64; N];
letmut inv =vec![1i64; N];
for i in1..N {
fact[i] = fact[i-1] * i asi64%MOD;
}
inv[N-1] = powmod(fact[N-1], MOD-2, MOD);
for i in (0..N-1).rev() {
inv[i] = inv[i+1] * (i asi64+1) %MOD;
}
let words: Vec<&str>= s.split_whitespace().collect();
letmut ans =1i64;
for w in words {
letmut cnt = [0; 26];
for c in w.chars() {
cnt[(c asu8-b'a') asusize] +=1;
}
letmut cur = fact[w.len()];
for&x in&cnt {
cur = cur * inv[x] %MOD;
}
ans = ans * cur %MOD;
}
ans asi32 }
}
fnpowmod(mut a: i64, mut b: i64, m: i64) -> i64 {
letmut res =1;
while b >0 {
if b &1==1 {
res = res * a % m;
}
a = a * a % m;
b >>=1;
}
res
}
⏰ Time complexity: O(L + W * D), where L is the length of s, W is the number of words, and D is the max word length. Precomputation is O(N), and each word is processed in O(D).
🧺 Space complexity: O(N), for factorials and inverses.