|
| 1 | +''' |
| 2 | +Given an array of strings products and a string searchWord. We want to design a system that suggests at most three product names from products after each character of searchWord is typed. Suggested products should have common prefix with the searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products. |
| 3 | +
|
| 4 | +Return list of lists of the suggested products after each character of searchWord is typed. |
| 5 | +
|
| 6 | + |
| 7 | +
|
| 8 | +Example 1: |
| 9 | +
|
| 10 | +Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse" |
| 11 | +Output: [ |
| 12 | +["mobile","moneypot","monitor"], |
| 13 | +["mobile","moneypot","monitor"], |
| 14 | +["mouse","mousepad"], |
| 15 | +["mouse","mousepad"], |
| 16 | +["mouse","mousepad"] |
| 17 | +] |
| 18 | +Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"] |
| 19 | +After typing m and mo all products match and we show user ["mobile","moneypot","monitor"] |
| 20 | +After typing mou, mous and mouse the system suggests ["mouse","mousepad"] |
| 21 | +Example 2: |
| 22 | +
|
| 23 | +Input: products = ["havana"], searchWord = "havana" |
| 24 | +Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]] |
| 25 | +Example 3: |
| 26 | +
|
| 27 | +Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags" |
| 28 | +Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]] |
| 29 | +Example 4: |
| 30 | +
|
| 31 | +Input: products = ["havana"], searchWord = "tatiana" |
| 32 | +Output: [[],[],[],[],[],[],[]] |
| 33 | + |
| 34 | +
|
| 35 | +Constraints: |
| 36 | +
|
| 37 | +1 <= products.length <= 1000 |
| 38 | +There are no repeated elements in products. |
| 39 | +1 <= Σ products[i].length <= 2 * 10^4 |
| 40 | +All characters of products[i] are lower-case English letters. |
| 41 | +1 <= searchWord.length <= 1000 |
| 42 | +All characters of searchWord are lower-case English letters. |
| 43 | +''' |
| 44 | +class TrieNode(object): |
| 45 | + def __init__(self): |
| 46 | + self.words = [] |
| 47 | + self.children = {} |
| 48 | + |
| 49 | +class Trie(object): |
| 50 | + def __init__(self): |
| 51 | + self.root = TrieNode() |
| 52 | + |
| 53 | + def insert(self, word): |
| 54 | + node = self.root |
| 55 | + for char in word: |
| 56 | + if char not in node.children: |
| 57 | + node.children[char] = TrieNode() |
| 58 | + node = node.children[char] |
| 59 | + node.words.append(word) |
| 60 | + node.words.sort() |
| 61 | + if len(node.words) > 3: |
| 62 | + node.words = node.words[:3] |
| 63 | + |
| 64 | + def search(self, word): |
| 65 | + result, node = [], self.root |
| 66 | + for char in word: |
| 67 | + if char not in node.children: |
| 68 | + break |
| 69 | + node = node.children[char] |
| 70 | + result.append(node.words[:]) |
| 71 | + for _ in range(len(word) - len(result)): |
| 72 | + result.append([]) |
| 73 | + return result |
| 74 | + |
| 75 | +class Solution(object): |
| 76 | + def suggestedProducts(self, products, searchWord): |
| 77 | + """ |
| 78 | + :type products: List[str] |
| 79 | + :type searchWord: str |
| 80 | + :rtype: List[List[str]] |
| 81 | + """ |
| 82 | + trie = Trie() |
| 83 | + for product in products: |
| 84 | + trie.insert(product) |
| 85 | + return trie.search(searchWord) |
0 commit comments