forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_226.py
58 lines (44 loc) · 1.53 KB
/
problem_226.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
def update_letter_order(sorted_words, letters):
order = list()
new_words = dict()
prev_char = None
for word in sorted_words:
if word:
char = word[0]
if char != prev_char:
order.append(char)
if char not in new_words:
new_words[char] = list()
new_words[char].append(word[1:])
prev_char = char
for index, char in enumerate(order):
letters[char] |= set(order[index + 1:])
for char in new_words:
update_letter_order(new_words[char], letters)
def find_path(letters, start, path, length):
if len(path) == length:
return path
if not letters[start]:
return None
for next_start in letters[start]:
new_path = find_path(letters, next_start, path + [next_start], length)
if new_path:
return new_path
def update_letter_order_helper(sorted_words):
letters = dict()
for word in sorted_words:
for letter in word:
if letter not in letters:
letters[letter] = set()
update_letter_order(sorted_words, letters)
max_children = max([len(x) for x in letters.values()])
potential_heads = [x for x in letters if len(letters[x]) == max_children]
path = None
for head in potential_heads:
path = find_path(letters, head, path=[head], length=len(letters))
if path:
break
return path
# Tests
assert update_letter_order_helper(
['xww', 'wxyz', 'wxyw', 'ywx', 'ywz']) == ['x', 'z', 'w', 'y']