[leetcode] 1268. Search Suggestions System

网友投稿 273 2022-09-14

[leetcode] 1268. Search Suggestions System

Description

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.

Return list of lists of the suggested products after each character of searchWord is typed.

Example 1:

Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"Output: [["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"]After typing m and mo all products match and we show user ["mobile","moneypot","monitor"]After typing mou, mous and mouse the system suggests ["mouse","mousepad"]

Example 2:

Input: products = ["havana"], searchWord = "havana"Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]

Example 3:

Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]

Example 4:

Input: products = ["havana"], searchWord = "tatiana"Output: [[],[],[],[],[],[],[]]

Constraints:

1 <= products.length <= 1000There are no repeated elements in products.1 <= Σ products[i].length <= 2 * 10^4All characters of products[i] are lower-case English letters.1 <= searchWord.length <= 1000All characters of searchWord are lower-case English letters.

分析

题目的意思是:给定一个字符串数组products,给定一个search word,找出search word每个前缀字符串能够匹配相同前缀排前3的products字符串,如果多于3个,则取前三就行。这道题我看了一下思路可以用二分查找,也可以用字典树。如果用字典树的话,除了拓展节点外,当前节点需要保存相同前缀排前3的字符串,在最后遍历searchword的时候,就能每走一步找到一个答案了哈,我也是第一次用前缀树做题,学习一下,哈哈。

二分查找要每次找出排序排前三的字符串,所以products需要提前排序好,然后再遍历searchword得到前缀,进行二分查找就行了。

代码

class Trie: def __init__(self): self.sub = {} self.suggestion = [] class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: root=Trie() for product in products: self.add(root,product) return self.search(root,searchWord) def add(self,tree,product): for ch in product: if(ch not in tree.sub): tree.sub[ch]=Trie() tree=tree.sub[ch] tree.suggestion.append(product) tree.suggestion.sort() if(len(tree.suggestion)>3): tree.suggestion.pop() def search(self,tree,product): ans=[] for ch in product: if(tree): tree=tree.sub.get(ch) if(tree): ans.append(tree.suggestion) else: ans.append([]) return ans

参考文献

​​[LeetCode] [Java/Python 3] Simple Trie and Binary Search codes w/ comment and brief analysis.​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:营销案例精选:完美日记收购Eve Lom?网友:它不配!
下一篇:[leetcode] 885. Spiral Matrix III
相关文章

 发表评论

暂时没有评论,来抢沙发吧~