-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
94 lines (73 loc) · 2.98 KB
/
__init__.py
File metadata and controls
94 lines (73 loc) · 2.98 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from typing import List, Set
from itertools import permutations
def num_tile_possibilities(tiles: str) -> int:
# Store unique sequences to avoid duplicates
unique_letter_sets: Set[str] = set()
# Sort characters to handle duplicates efficiently
sorted_tiles: str = "".join(sorted(tiles))
def generate_sequences(current: str, index: int):
# Recursively generates all possible sequences by including/excluding each tile.
# Once a new sequence is found, its unique permutations are counted.
if index >= len(sorted_tiles):
# If a new sequence is found, count its unique permutations
if current not in unique_letter_sets:
unique_letter_sets.add(current)
total_permutations = len(set(permutations(current)))
return total_permutations
return 0
# Explore two possibilities: skipping the current character or including it
without_letter = generate_sequences(current, index=index + 1)
with_letter = generate_sequences(current + sorted_tiles[index], index=index + 1)
# Return all the possible sequences
return without_letter + with_letter
# Optionally, you can write the count_permutations and the factorial method if not using builtin methods
# def factorial(n):
#
# # Computes the factorial of a given number.
# if n <= 1:
# return 1
#
# result = 1
# for num in range(2, n + 1):
# result *= num
# return result
#
# def count_permutations(sequence):
#
# # Calculates the number of unique permutations of a sequence, accounting for duplicate characters.
# permutations = factorial(len(sequence))
#
# # Adjust for repeated characters by dividing by factorial of their counts
# for count in Counter(sequence).values():
# permutations //= factorial(count)
#
# return permutations
output = generate_sequences("", 0)
return output - 1
def num_tile_possibilities_with_recursion(tiles: str) -> int:
sequences: Set[str] = set()
used: List[bool] = [False] * len(tiles)
def generate_sequences(current: str) -> None:
sequences.add(current)
for idx, char in enumerate(tiles):
if not used[idx]:
used[idx] = True
generate_sequences(current + char)
used[idx] = False
generate_sequences("")
return len(sequences) - 1
def num_tile_possibilities_with_optimized_recursion(tiles: str) -> int:
char_count: List[int] = [0] * 26
for letter in tiles:
char_count[ord(letter) - ord("A")] += 1
def generate_sequences() -> int:
result = 0
for idx in range(26):
if char_count[idx] == 0:
continue
result += 1
char_count[idx] -= 1
result += generate_sequences()
char_count[idx] += 1
return result
return generate_sequences()