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 }

      

  • 相关阅读:
    数据分析的广阔前景
    验证驱动的调用者
    重构:改善既有代码的设计 精彩书评一
    谁说菜鸟不会数据分析
    编写安全的驱动程序之输入输出检查
    数据分析师的基本素质
    重构:改善既有代码的设计 精彩书评二
    Java编程思想(第4版)(评注版)
    第四章(2)Libgdx手工项目设置
    第四章(1)Libgdx项目安装、运行和调试
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12190154.html
Copyright © 2011-2022 走看看