Grokking Data Structures & Algorithms for Coding Interviews
0% completed
Search Suggestions System (medium)
.....
.....
.....
Like the course? Get enrolled and start learning!
A
Anonymous
· 2 years ago
class Node: def __init__(self, val): self.val = val self.children = {} self.isEnd = False class Solution: def suggestedProducts(self, products, searchWord): # build trie root = Node('*') for product in products: node = root for char in product: if char not in node.children: node.children[char] = Node(char) node = node.children[char] node.isEnd = True # dfs on the trie def dfs(node, path, result): if node.isEnd: result.append(''.join(path)) for child_node_char, child_node in node.children.items(): path.append(child_node_char) df
A
Anonymous
· 3 years ago
Input:
["mobile","mouse","moneypot","monitor","mousepad"]
"mouse"
Output
[["mobile","mouse","mousepad","moneypot","monitor"],["mobile","mouse","mousepad","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
Expected
[["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
For prefix "m", all 5 words should be showed, do I understand it correctly?
Show 1 reply
Reading Progress
0%