forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-anagrams.py
47 lines (40 loc) · 1.29 KB
/
count-anagrams.py
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
37
38
39
40
41
42
43
44
45
46
47
# Time: O(n)
# Space: O(n)
import collections
# combinatorics
class Solution(object):
def countAnagrams(self, s):
"""
:type s: str
:rtype: int
"""
MOD = 10**9+7
fact, inv, inv_fact = [[1]*2 for _ in xrange(3)]
def lazy_init(n):
while len(inv) <= n: # lazy initialization
fact.append(fact[-1]*len(inv) % MOD)
inv.append(inv[MOD%len(inv)]*(MOD-MOD//len(inv)) % MOD) # https://cp-algorithms.com/algebra/module-inverse.html
inv_fact.append(inv_fact[-1]*inv[-1] % MOD)
def factorial(n):
lazy_init(n)
return fact[n]
def inv_factorial(n):
lazy_init(n)
return inv_fact[n]
def count(j, i):
result = 1
cnt = collections.Counter()
for k in xrange(j, i+1):
cnt[s[k]] += 1
result = factorial(sum(cnt.itervalues()))
for c in cnt.itervalues():
result = (result*inv_factorial(c))%MOD
return result
result = 1
j = 0
for i in xrange(len(s)):
if i+1 != len(s) and s[i+1] != ' ':
continue
result = (result*count(j, i))%MOD
j = i+2
return result