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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值