题目如下:

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
  • 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排序,接下来把每个product存入字典树中,字典树每个节点除了保存子节点的地址之外,还存前三个前缀能映射到该节点的product。

代码如下:

class TreeNode(object):
def __init__(self,val):
self.val = val
self.child = {}
self.top3 = []
class Trie(object):
def __init__(self):
self.root = TreeNode(None)
def add(self,word):
node = self.root
for i in word:
if i not in node.child:
child_node = TreeNode(i)
node.child[i] = child_node
if len(node.top3) < 3:
node.top3.append(word)
node = node.child[i]
if len(node.top3) < 3:
node.top3.append(word) def search(self,prefix):
node = self.root
for i in prefix:
if i not in node.child:
return []
node = node.child[i]
return node.top3 class Solution(object):
def suggestedProducts(self, products, searchWord):
"""
:type products: List[str]
:type searchWord: str
:rtype: List[List[str]]
"""
products.sort()
trie = Trie()
for product in products:
trie.add(product)
res = []
word = ''
for char in searchWord:
word += char
res.append(trie.search(word)) return res

最新文章

  1. Javascript中两个等于号和三个等于号的区别(==/===)
  2. 分离与继承的思想实现图片上传后的预览功能:ImageUploadView
  3. NOIP2014提高组解方程
  4. iOS开发--遇到ARGB/RGBA怎么办
  5. SQL Server中的锁的简单学习
  6. Unity 5.3 安装完没有Android(安卓)或IOS Module(模块)?
  7. Lc.exe已退出,代码为-1
  8. 括号配对nyoj2(疑问)
  9. winform继承窗体,无法修改父窗体控件问题处理笔记
  10. POJ 3258 River Hopscotch (binarysearch)
  11. 第八篇、封装NSURLSession网络请求框架
  12. CoreBluetooth - 中心模式
  13. android ListView内数据的动态添加与删除
  14. vue-10-路由
  15. bzoj 3620 暴力KMP
  16. TCP/UDP OSI_layer 4
  17. SSL连接并非完全问题解决
  18. 二、spark SQL交互scala操作示例
  19. 彻底澄清c/c++指针概念
  20. FFM

热门文章

  1. TensorFlow实战第七课(dropout解决overfitting)
  2. webdriervAPI(窗口截图)
  3. s7-200日常使用烂笔头
  4. MSF魔鬼训练营-5.3 MS08-067安全漏洞实战
  5. mac环境提示:make sure that /usr/local/bin is in your path
  6. 小记--------CDH版本启动cloudera manager UI界面
  7. Codeforces 1201C. Maximum Median
  8. VMware Ubuntu18.04 安装及配置笔记
  9. Auto-increment 自动增长
  10. golang(1):简介