【leetcode】1268. Search Suggestions System
题目如下:
Given an array of strings
productsand a stringsearchWord. We want to design a system that suggests at most three product names fromproductsafter each character ofsearchWordis 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
productsafter each character ofsearchWordis 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 <= 10001 <= Σ products[i].length <= 2 * 10^4- All characters of
products[i]are lower-case English letters.1 <= searchWord.length <= 1000- All characters of
searchWordare 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
【leetcode】1268. Search Suggestions System的更多相关文章
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- 【LeetCode】1166. Design File System 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 目录树 日期 题目地址https://leetc ...
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】700. Search in a Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【leetcode】Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- 【linux杂谈】查看centOS系统的版本号和内核号
因为种种原因,我们通常需要查看centOS系统的版本号和内核号. 这里以centOS 6为切入点,展示了几种查看版本号和内核号的方法,同时也验证了其在centOS 7上的可行性. 一.centOS 6 ...
- 系统的可用性用平均无故障时间( MTTF)
计算机系统的可用性用平均无故障时间( MTTF)来度量,即计算机系统平均能够正常运行多长时间,才发生一次故障.系统的可用性越高,平均无故障时间越长. 可维护性用平均维修时间(MTTR)来度量,即系统发 ...
- 阿里云Open API自动化脚本—ECS公网IP转化弹性公网IP
1.OpenAPI Explorer 记录一下使用阿里云 Open API 自动化/脚本化 “ECS 公网 IP 转化弹性公网 IP”的实现 全过程.原博客地址:https://www.markedi ...
- MYSQL—第二部分(Linux版本的安装和数据表的操作)
Linux版本的安装(过于简单了) 安装: ? 1 yum install mysql-server 服务端启动 ? 1 mysql.server start 客户端连接 ? 1 2 3 4 5 6 ...
- P1816忠诚
这是一个区间查询最值的问题,用线段树来做. 建树的时候,这里不是求和,应该是e[k].w=min(e[k*2].w,e[k*2+1].w),所以这里要注意以下,其次是查询的时候,因为本题不用让我们修改 ...
- python线程中的同步问题
多线程开发可能遇到的问题 假设两个线程t1和t2都要对num=0进行增1运算,t1和t2都各对num修改1000000次,num的最终的结果应该为2000000.但是由于是多线程访问,有可能出现下面情 ...
- jinja2介绍
jinja2介绍 jinja2是Flask作者开发的一个模板系统,起初是仿django模板的一个模板引擎,为Flask提供模板支持,由于其灵活,快速和安全等优点被广泛使用. jinja2的优点 jin ...
- Hive 教程(七)-DML基础
DML,Hive Data Manipulation Language,数据操作语言: 通俗理解就是数据库里与数据的操作,如增删改查,统计汇总等: Loading files into tables ...
- maven中scope属性的
Dependency Scope 在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: * c ...
- DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension)
DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension) 难度: ★☆☆☆☆(1星) 适用范围: ★★★★★(5星) 这个时间系列想 ...