-
Notifications
You must be signed in to change notification settings - Fork 3
/
2514.cpp
36 lines (35 loc) · 1.07 KB
/
2514.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
unordered_map<long long, long long> mp;
long long inv(long long a, long long mod) {
if (mp.find(a) != mp.end()) return mp[a];
if (a == 1) return 1;
return mp[a] = (mod - mod / a) * inv(mod % a, mod) % mod;
}
int countAnagrams(string s) {
stringstream ss(s);
string temp;
long long ans = 1;
long long mod = 1e9 + 7;
vector<int> counts(26, 0);
while (getline(ss, temp, ' ')) {
for (int i = 0; i < 26; ++i) counts[i] = 0;
int total = temp.size();
for (int i = 0; i < total; ++i) counts[temp[i] - 'a']++;
long long sum = 1;
for (int i = 1; i <= total; ++i) {
sum *= i;
sum %= mod;
}
for (int i = 0; i < 26; ++i) {
for (int j = 2; j <= counts[i]; ++j) {
sum *= inv(j, mod);
sum %= mod;
}
}
ans *= sum;
ans %= mod;
}
return ans;
}
};