-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathletter-tile-possibilities.py
More file actions
34 lines (25 loc) · 1011 Bytes
/
Copy pathletter-tile-possibilities.py
File metadata and controls
34 lines (25 loc) · 1011 Bytes
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
import math
from collections import Counter
class Solution:
def numTilePossibilities(self, tiles: str) -> int:
combinations = set()
def calculate_combinations(
arr: list[str], start: int, prev: list[str], length: int
) -> None:
if len(prev) == length:
combinations.add("".join(prev))
return
for pos in range(start, len(arr)):
prev.append(arr[pos])
calculate_combinations(arr, pos + 1, prev, length)
prev.pop()
tiles_sorted = list(sorted(tiles))
for length in range(1, len(tiles_sorted) + 1):
calculate_combinations(tiles_sorted, 0, [], length)
result = 0
for combination in combinations:
sub_result = math.factorial(len(combination))
for count in Counter(combination).values():
sub_result /= math.factorial(count)
result += int(sub_result)
return result