ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 1268. Search Suggestions System

2022-07-13 04:33:37  阅读:203  来源: 互联网

标签:node Node Search 1268 suggestion Suggestions products searchWord root


You are given an array of strings products and a string searchWord.

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 searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.

Return a 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"]]

Constraints:

  • 1 <= products.length <= 1000
  • 1 <= products[i].length <= 3000
  • 1 <= sum(products[i].length) <= 2 * 104
  • All the strings of products are unique.
  • products[i] consists of lowercase English letters.
  • 1 <= searchWord.length <= 1000
  • searchWord consists of lowercase English letters.

搜索推荐系统。

给你一个产品数组 products 和一个字符串 searchWord ,products  数组中每个产品都是一个字符串。

请你设计一个推荐系统,在依次输入单词 searchWord 的每一个字母后,推荐 products 数组中前缀与 searchWord 相同的最多三个产品。如果前缀相同的可推荐产品超过三个,请按字典序返回最小的三个。

请你以二维列表的形式,返回在输入 searchWord 每个字母后相应的推荐产品的列表。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/search-suggestions-system
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题题意很直观,我提供一个字典树的做法,参考了这个帖子

代码不难理解,有几个细节提示一下。对于 products 数组里的每个单词,我们都把它 insert 到字典树里。因为之后我们在做 search 的时候需要返回相关的前缀 prefix,所以我们在 insert 的时候,每添加一个字母,我们就把当前这个 product 放入当前这个前缀的 suggestion 里,并一直保持 suggestion 是排序的且长度最大为 3。这样我们就能保证当我们 search 的时候,能返回字典序最小的三个 suggestion。

时间O(mn) - 创建字典树 + insert 每个单词

空间O(n)

Java实现

 1 class Solution {
 2     public List<List<String>> suggestedProducts(String[] products, String searchWord) {
 3         Node root = new Node();
 4         for (String p : products) {
 5             insert(p, root);
 6         }
 7         return search(searchWord, root);
 8     }
 9 
10     private void insert(String product, Node root) {
11         Node node = root;
12         for (int i = 0; i < product.length(); i++) {
13             int j = product.charAt(i) - 'a';
14             if (node.children[j] == null) {
15                 node.children[j] = new Node();
16             }
17             node = node.children[j];
18             node.suggestion.offer(product);
19             Collections.sort(node.suggestion);
20             if (node.suggestion.size() > 3) {
21                 node.suggestion.pollLast();
22             }
23         }
24     }
25 
26     private List<List<String>> search(String searchWord, Node root) {
27         List<List<String>> res = new ArrayList<>();
28         for (char c : searchWord.toCharArray()) {
29             if (root != null) {
30                 root = root.children[c - 'a'];
31             }
32             res.add(root == null ? Arrays.asList() : root.suggestion);
33         }
34         return res;
35     }
36 }
37 
38 class Node {
39     Node[] children = new Node[26];
40     LinkedList<String> suggestion = new LinkedList<>();
41 }

 

LeetCode 题目总结

标签:node,Node,Search,1268,suggestion,Suggestions,products,searchWord,root
来源: https://www.cnblogs.com/cnoodle/p/16472416.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有