LeetCode(1268):搜索推薦系統 Search Suggestions System(Java)

2019.12.9 LeetCode 從零單刷個人筆記整理(持續更新)

github:https://github.com/ChopinXBP/LeetCode-Babel

這是參加的第二場周賽。

本題很直接可以想到前綴樹+DFS的思路,不過要注意一點神坑:測試用例的字典中可能包含重複的單詞。應對這點,只需要在字典樹結點中加上條件count用於統計相同單詞的出現次數即可。

也可以不直接建樹,先將字典單詞排序,每次遍歷前綴符合的單詞,保留集合以供下一輪遍歷。多次重複遍歷即可得出結果。


傳送門:搜索推薦系統

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.

給你一個產品數組 products 和一個字符串 searchWord ,products 數組中每個產品都是一個字符串。

請你設計一個推薦系統,在依次輸入單詞 searchWord 的每一個字母后,推薦 products 數組中前綴與 searchWord 相同的最多三個產品。如果前綴相同的可推薦產品超過三個,請按字典序返回最小的三個。

請你以二維列表的形式,返回在輸入 searchWord 每個字母后相應的推薦產品的列表。

示例 1:
輸入:products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
輸出:[
["mobile","moneypot","monitor"],
["mobile","moneypot","monitor"],
["mouse","mousepad"],
["mouse","mousepad"],
["mouse","mousepad"]
]
解釋:按字典序排序後的產品列表是 ["mobile","moneypot","monitor","mouse","mousepad"]
輸入 m 和 mo,由於所有產品的前綴都相同,所以系統返回字典序最小的三個產品 ["mobile","moneypot","monitor"]
輸入 mou, mous 和 mouse 後系統都返回 ["mouse","mousepad"]

示例 2:
輸入:products = ["havana"], searchWord = "havana"
輸出:[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]

示例 3:
輸入:products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
輸出:[["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]

示例 4:
輸入:products = ["havana"], searchWord = "tatiana"
輸出:[[],[],[],[],[],[],[]]

提示:
1 <= products.length <= 1000
1 <= Σ products[i].length <= 2 * 10^4
products[i] 中所有的字符都是小寫英文字母。
1 <= searchWord.length <= 1000
searchWord 中所有字符都是小寫英文字母。


import java.util.ArrayList;
import java.util.List;

/**
 *
 * 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.
 * 給你一個產品數組 products 和一個字符串 searchWord ,products  數組中每個產品都是一個字符串。
 * 請你設計一個推薦系統,在依次輸入單詞 searchWord 的每一個字母后,推薦 products 數組中前綴與 searchWord 相同的最多三個產品。如果前綴相同的可推薦產品超過三個,請按字典序返回最小的三個。
 * 請你以二維列表的形式,返回在輸入 searchWord 每個字母后相應的推薦產品的列表。
 *
 */

public class SearchSuggestionsSystem {
    private class TrieNode{
        boolean end = false;
        String str = null;
        int count = 0;
        TrieNode[] children = new TrieNode[26];
    }

    private class Trie{
        TrieNode root = new TrieNode();
        public void insert(String[] products){
            for(String str : products){
                insertWord(str);
            }
        }
        private void insertWord(String products){
            TrieNode node = root;
            for(char c : products.toCharArray()){
                if(node.children[c - 'a'] == null){
                    node.children[c - 'a'] = new TrieNode();
                }
                node = node.children[c - 'a'];
            }
            if(node.end != true){
                node.end = true;
                node.str = products;
            }
            node.count++;
        }
        public List<List<String>> searchWord(String word){
            List<List<String>> result = new ArrayList<>();
            for(int i = 1; i <= word.length(); i++){
                result.add(search(word.substring(0, i)));
            }
            return result;
        }
        private List<String> search(String pattern){
            List<String> result = new ArrayList<>();
            TrieNode node = root;
            for(char c : pattern.toCharArray()){
                if(node.children[c - 'a'] == null){
                    return result;
                }
                node = node.children[c - 'a'];
            }
            Solution(node, result);
            return result;
        }
        private void Solution(TrieNode root, List<String> result){
            if(root.end){
                for(int i = 0; i < root.count; i++){
                    result.add(root.str);
                    if(result.size() == 3){
                        return;
                    }
                }
            }
            for(TrieNode node : root.children){
                if(node != null){
                    Solution(node, result);
                }
                if(result.size() == 3){
                    return;
                }
            }
        }
    }

    public List<List<String>> suggestedProducts(String[] products, String searchWord) {
        Trie trie = new Trie();
        trie.insert(products);
        return trie.searchWord(searchWord);
    }
}




#Coding一小時,Copying一秒鐘。留個言點個讚唄,謝謝你#

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章