首页 > 其他 > 详细

LeetCode 1268. Search Suggestions System

时间:2020-01-14 09:03:45      阅读:87      评论:0      收藏:0      [点我收藏+]

原题链接在这里:https://leetcode.com/problems/search-suggestions-system/

题目:

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.

题解:

When typing each character of search word, we want to give out top 3 words with the same prefix.

We could sort the product words and words with same prefix would be together.

Thus we first sort the product words and put them in a TreeMap with value as its index in the array.

Later for each character, we find the prefix, and with this prefix, we use ceiling key as the start product word.

And use floor key, prefix + "~", a char that is larger than z, to find out the ending place with this prefix.

Then we take starting index to Math.min(starting index + 3, ending index + 1) sub list. and add to res.

Time Complexity(n*logn + m*logn). n = products.length. m = searchWord.length().

Space: O(n).

AC Java: 

 1 class Solution {
 2     public List<List<String>> suggestedProducts(String[] products, String searchWord) {
 3         List<List<String>> res = new ArrayList<>();
 4         if(products == null || searchWord == null){
 5             return res;
 6         }
 7         
 8         Arrays.sort(products);
 9         List<String> proList = Arrays.asList(products);
10         TreeMap<String, Integer> proToIndex = new TreeMap<>();
11         for(int i = 0; i < products.length; i++){
12             proToIndex.put(products[i], i);
13         }
14         
15         String key = "";
16         for(char c : searchWord.toCharArray()){
17             key = key + c;
18             String ceil = proToIndex.ceilingKey(key);
19             String floor = proToIndex.floorKey(key + "~");
20             if(ceil == null || floor == null){
21                 break;
22             }
23             
24             res.add(proList.subList(proToIndex.get(ceil), Math.min(proToIndex.get(ceil) + 3, proToIndex.get(floor) + 1)));
25         }
26         
27         while(res.size() < searchWord.length()){
28             res.add(new ArrayList<String>());
29         }
30         
31         return res;
32     }
33 }

 

 

LeetCode 1268. Search Suggestions System

原文:https://www.cnblogs.com/Dylan-Java-NYC/p/12190154.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!