-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC0371.py
46 lines (37 loc) · 1.42 KB
/
LC0371.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
class TrieNode:
def __init__(self):
self.isEnd = False
self.children = [None] * 26
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
current = self.root
for c in word:
if current.children[ord(c) - ord("a")] is None:
current.children[ord(c) - ord("a")] = TrieNode()
current = current.children[ord(c) - ord("a")]
current.isEnd = True
# Find the shortest root of the word in the trie
def shortest_root(self, word):
current = self.root
for i in range(len(word)):
c = word[i]
if current.children[ord(c) - ord("a")] is None:
# There is not a corresponding root in the trie
return word
current = current.children[ord(c) - ord("a")]
if current.isEnd:
return word[: i + 1]
# There is not a corresponding root in the trie
return word
class Solution:
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
word_array = sentence.split()
dict_trie = Trie()
for word in dictionary:
dict_trie.insert(word)
# Replace each word in the sentence with the corresponding shortest root
for word in range(len(word_array)):
word_array[word] = dict_trie.shortest_root(word_array[word])
return " ".join(word_array)