-
Notifications
You must be signed in to change notification settings - Fork 0
/
greedy.py
46 lines (33 loc) · 1009 Bytes
/
greedy.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
from collections import Counter
from functools import reduce
def anagram(line):
len_line = len(line)
ct = Counter(line)
def fn(idx):
if idx == len_line:
return 0
ret = 0
for key, val in ct.items():
if val == 0:
continue
if key < line[idx]:
ct[key] -= 1
curr = reduce(lambda x, y: x * y, range(1, len_line - idx))
for val in ct.values():
if val >= 2:
curr //= reduce(lambda x, y: x * y, range(2, val + 1))
ret += curr
ct[key] += 1
elif key == line[idx]:
ct[key] -= 1
ret += fn(idx + 1)
ct[key] += 1
return ret
return fn(0) + 1
# with open("anagram.in") as f:
# line = f.readline().strip()
line = input().strip()
print(anagram(line))
# print(anagram("EARTH"))
# print(anagram("HEART"))
# print(anagram("IOI"))