-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (44 loc) · 2.18 KB
/
main.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
# 648. Replace Words
# In English, we have a concept called root, which can be followed by some other words to form another longer word - let's call this word successor. For example, when the root "an" is followed by the successor word "other", we can form a new word "another".
# Given a dictionary consisting of many roots and a sentence consisting of words spearted by spaces. You need to replace all the successors in the sentence with the root forming it. If a successor can be replaced by more than one root, replace it with the root with the shortest length.
# Return the sentence after the replacement.
class TrieNode:
def __init__(self, letter):
self.letter = letter
self.isFullWord = False
self.children = {}
class Trie:
def __init__(self):
self.root = TrieNode("")
def insert(self, word) -> None:
cur = self.root
for letter in word:
if letter not in cur.children:
cur.children[letter] = TrieNode(letter)
cur = cur.children[letter]
cur.isFullWord = True
def findSuccessor(self, word) -> str:
cur = self.root
i = 0
for letter in word:
# No prefix found
if letter not in cur.children:
return word
# As soon as we see a matching prefix, return immediately because
# we need to find root with the shortest length
if cur.children[letter].isFullWord:
return word[0:i + 1]
i += 1
cur = cur.children[letter]
return word
class Solution:
def replaceWords(self, dictionary: List[str], sentence: str) -> str:
trie = Trie()
# Time complexity: O(m * n) where m is num of roots, n is max root length
for item in dictionary:
trie.insert(item)
result = []
# Time complexity: o(n * s) where n is the max length of root, s is number of words in sentence
for word in sentence.split():
result.append(trie.findSuccessor(word))
return " ".join(result)