-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_common_prefix.py
64 lines (47 loc) · 1.46 KB
/
max_common_prefix.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
"""
Given a list of words, find the maximum common prefix between
any 2 words in the input list.
Problem stated at this video: https://youtu.be/am3fa6otsE8?t=1186
"""
class TrieNode:
def __init__(self):
self.children = dict()
def findLongestCommonPrefix(words):
max_prefix = ""
root = TrieNode()
for word in words:
current_node = root
current_prefix = ""
for char in word:
if char in current_node.children:
current_prefix += char
if len(current_prefix) > len(max_prefix):
max_prefix = current_prefix
current_node = current_node.children[char]
else:
new_node = TrieNode()
current_node.children[char] = new_node
current_node = current_node.children[char]
return max_prefix
def validate(words, expected_answer):
answer = findLongestCommonPrefix(words)
if answer != expected_answer:
raise Exception(
f"Wrong answer '{answer}'. Expected answer is: '{expected_answer}'"
)
if __name__ == "__main__":
validate(["electronics"], "")
validate(["electronics", "electronicz"], "electronic")
validate([], "")
validate(
[
"electronics",
"compact disc",
"alphabet",
"compass",
"giraffe",
"electricity",
],
"electr",
)
print("All tests passed!")