-
Notifications
You must be signed in to change notification settings - Fork 0
/
264.py
75 lines (58 loc) · 2.1 KB
/
264.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
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
"""
Problem:
Given a set of characters C and an integer k, a De Bruijn sequence is a cyclic sequence
in which every possible k-length string of characters in C occurs exactly once.
For example, suppose C = {0, 1} and k = 3. Then our sequence should contain the
substrings {'000', '001', '010', '011', '100', '101', '110', '111'}, and one possible
solution would be 00010111.
Create an algorithm that finds a De Bruijn sequence.
"""
from typing import List, Set
def generate_all_combinations(
characters: Set[str], size: int, accumulator: List[str]
) -> None:
if not accumulator:
accumulator.extend(characters)
size -= 1
while size > 0:
updated_acc = []
for _ in range(len(accumulator)):
temp = accumulator.pop(0)
for char in characters:
updated_acc.append(temp + char)
size -= 1
accumulator.extend(updated_acc)
def get_de_bruijn_helper(
characters: Set[str], combinations_set: Set[str], k: int, context: str = ""
) -> Set[str]:
if not combinations_set:
return set([context])
dseqs = set()
if not context:
# if context is empty, it is initized using a combination
for combo in combinations_set:
child_dseqs = get_de_bruijn_helper(
characters, combinations_set - set([combo]), k, combo
)
dseqs |= child_dseqs
return dseqs
for character in characters:
combo = context[-(k - 1) :] + character
if combo in combinations_set:
child_dseqs = get_de_bruijn_helper(
characters, combinations_set - set([combo]), k, context + character
)
dseqs |= child_dseqs
return dseqs
def get_de_bruijn(characters: Set[str], k: int) -> Set[str]:
combinations_list = []
generate_all_combinations(characters, k, combinations_list)
combinations_set = set(combinations_list)
return get_de_bruijn_helper(characters, combinations_set, k)
if __name__ == "__main__":
print(get_de_bruijn({"0", "1"}, 3))
"""
SPECS:
TIME COMPLEXITY: O(n ^ k)
SPACE COMPLEXITY: O(n + k)
"""