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 }

      

  • 相关阅读:
    软件配置管理的作用?软件配置包括什么?
    火火恍恍惚惚
    什么是软件测试?软件测试的目的与原则
    软件生存周期及其模型是什么?
    试述软件的概念和特点?软件复用的含义?构件包括哪些?
    一台客户端有三百个客户与三百个客户端有三百个客户对服务器施压,有什么区别?
    numpy的broadcast是怎么做的
    python到底是解释型语言还是需要编译的?
    python:删除类实例,仅仅只有动态属性会被删除,类属性不会被删除
    jupyter的kernel莫名其妙找不到,莫名其妙就中断
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12190154.html
Copyright © 2011-2022 走看看