Design a search autocomplete system for a search engine. Users may input a sentence (at least one word and end with a special character '#'
).
You are given a string array sentences
and an integer array times
both of length n
where sentences[i]
is a previously typed sentence and times[i]
is the corresponding number of times the sentence was typed. For each input character except '#'
, return the top 3
historical hot sentences that have the same prefix as the part of the sentence already typed.
Here are the specific rules:
- The hot degree for a sentence is defined as the number of times a user typed the exactly same sentence before.
- The returned top
3
hot sentences should be sorted by hot degree (The first is the hottest one). If several sentences have the same hot degree, use ASCII-code order (smaller one appears first). - If less than
3
hot sentences exist, return as many as you can. - When the input is a special character, it means the sentence ends, and in this case, you need to return an empty list.
Implement the AutocompleteSystem
class:
AutocompleteSystem(String[] sentences, int[] times)
Initializes the object with thesentences
andtimes
arrays.List<String> input(char c)
This indicates that the user typed the characterc
.- Returns an empty array
[]
ifc == '#'
and stores the inputted sentence in the system. - Returns the top
3
historical hot sentences that have the same prefix as the part of the sentence already typed. If there are fewer than3
matches, return them all.
- Returns an empty array
Example 1:
Input
["AutocompleteSystem", "input", "input", "input", "input"]
[[["i love you", "island", "iroman", "i love leetcode"], [5, 3, 2, 2]], ["i"], [" "], ["a"], ["#"]]
Output
[null, ["i love you", "island", "i love leetcode"], ["i love you", "i love leetcode"], [], []]
Explanation
AutocompleteSystem obj = new AutocompleteSystem(["i love you", "island", "iroman", "i love leetcode"], [5, 3, 2, 2]);
obj.input("i"); // return ["i love you", "island", "i love leetcode"]. There are four sentences that have prefix "i". Among them, "ironman" and "i love leetcode" have same hot degree. Since ' ' has ASCII code 32 and 'r' has ASCII code 114, "i love leetcode" should be in front of "ironman". Also we only need to output top 3 hot sentences, so "ironman" will be ignored.
obj.input(" "); // return ["i love you", "i love leetcode"]. There are only two sentences that have prefix "i ".
obj.input("a"); // return []. There are no sentences that have prefix "i a".
obj.input("#"); // return []. The user finished the input, the sentence "i a" should be saved as a historical sentence in system. And the following input will be counted as a new search.
半懂不懂,随便抄抄吧
class AutocompleteSystem { private HashMap<String, Integer> map; private StringBuilder sb; private TrieNode root; private TrieNode current; public AutocompleteSystem(String[] sentences, int[] times) { map = new HashMap<>(); sb = new StringBuilder(); root = new TrieNode(); current = root; for (int i = 0; i < sentences.length; i++) { map.put(sentences[i], times[i]); root.add(sentences[i], 0); } } //每次input都返回top 3 public List<String> input(char c) { if (c == '#') { String str = sb.toString(); map.put(str, map.getOrDefault(str, 0) + 1); root.add(str, 0); sb = new StringBuilder(); current = root; return new ArrayList<>(); } sb.append(c); current = current.getNext(c); return current.top3; } private class TrieNode { private List<String> top3; private TrieNode[] next; private TrieNode() { this.top3 = new ArrayList<>(); this.next = new TrieNode[27]; } //每次add的时候都sort private void add(String s, int start) { if (!top3.contains(s)) top3.add(s); Collections.sort(top3, (a, b) -> { if (map.get(a) == map.get(b)) return a.compareTo(b); return map.get(b) - map.get(a); }); if (top3.size() > 3) top3.remove(top3.size() - 1); if (start == s.length()) return; getNext(s.charAt(start)).add(s, start + 1); } private TrieNode getNext(char ch) { int index = ch == ' ' ? 26 : ch - 'a'; if (next[index] == null) next[index] = new TrieNode(); return next[index]; } } }