zoukankan      html  css  js  c++  java
  • LeetCode 1268. Search Suggestions System

    原题链接在这里: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.

    Note: subList(l, r) left inclusive and right exclusive. if l == r, return empty list. 

    When it no longer has this prefix, ceiling key is next larger key, e.g. "abd" -> "ddd", floor key still "abd", then floor key index + 1 == ceiling key index.

    Add empty list to result. 

    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 }

      

  • 相关阅读:
    linux 文件系统(inode和block)
    vue状态管理vuex从浅入深详细讲解
    小白都能看懂的vue中各种通信传值方式,附带详细代码
    CSS3移动端vw+rem不依赖JS实现响应式布局
    JavaScript原生封装ajax请求和Jquery中的ajax请求
    永久解决Sublime包管理package control 打开install package报错 There are no packages available for installation
    从GitLab上创建分支本地拉取项目和提交项目详解
    前端路由的两种实现方式,内附详细代码
    几个例子理解浅拷贝和深拷贝
    讲解JavaScript中对闭包的理解
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12190154.html
Copyright © 2011-2022 走看看