输入
["AutocompleteSystem", "input", "input", "input", "input"]
[[["i love you", "island", "iroman", "i love leetcode"], [5, 3, 2, 2]], ["i"], [" "], ["a"], ["#"]]
输出
[null, ["i love you", "island", "i love leetcode"], ["i love you", "i love leetcode"], [], []]
解释
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.
classTrie:def__init__(self):self.children=[None]*27self.v=0self.w=''definsert(self,w,t):node=selfforcinw:idx=26ifc==' 'elseord(c)-ord('a')ifnode.children[idx]isNone:node.children[idx]=Trie()node=node.children[idx]node.v+=tnode.w=wdefsearch(self,pref):node=selfforcinpref:idx=26ifc==' 'elseord(c)-ord('a')ifnode.children[idx]isNone:returnNonenode=node.children[idx]returnnodeclassAutocompleteSystem:def__init__(self,sentences:List[str],times:List[int]):self.trie=Trie()fora,binzip(sentences,times):self.trie.insert(a,b)self.t=[]definput(self,c:str)->List[str]:defdfs(node):ifnodeisNone:returnifnode.v:res.append((node.v,node.w))fornxtinnode.children:dfs(nxt)ifc=='#':s=''.join(self.t)self.trie.insert(s,1)self.t=[]return[]res=[]self.t.append(c)node=self.trie.search(''.join(self.t))ifnodeisNone:returnresdfs(node)res.sort(key=lambdax:(-x[0],x[1]))return[v[1]forvinres[:3]]# Your AutocompleteSystem object will be instantiated and called as such:# obj = AutocompleteSystem(sentences, times)# param_1 = obj.input(c)
classTrie{Trie[]children=newTrie[27];intv;Stringw="";voidinsert(Stringw,intt){Trienode=this;for(charc:w.toCharArray()){intidx=c==' '?26:c-'a';if(node.children[idx]==null){node.children[idx]=newTrie();}node=node.children[idx];}node.v+=t;node.w=w;}Triesearch(Stringpref){Trienode=this;for(charc:pref.toCharArray()){intidx=c==' '?26:c-'a';if(node.children[idx]==null){returnnull;}node=node.children[idx];}returnnode;}}classAutocompleteSystem{privateTrietrie=newTrie();privateStringBuildert=newStringBuilder();publicAutocompleteSystem(String[]sentences,int[]times){inti=0;for(Strings:sentences){trie.insert(s,times[i++]);}}publicList<String>input(charc){List<String>res=newArrayList<>();if(c=='#'){trie.insert(t.toString(),1);t=newStringBuilder();returnres;}t.append(c);Trienode=trie.search(t.toString());if(node==null){returnres;}PriorityQueue<Trie>q=newPriorityQueue<>((a,b)->a.v==b.v?b.w.compareTo(a.w):a.v-b.v);dfs(node,q);while(!q.isEmpty()){res.add(0,q.poll().w);}returnres;}privatevoiddfs(Trienode,PriorityQueueq){if(node==null){return;}if(node.v>0){q.offer(node);if(q.size()>3){q.poll();}}for(Trienxt:node.children){dfs(nxt,q);}}}/** * Your AutocompleteSystem object will be instantiated and called as such: * AutocompleteSystem obj = new AutocompleteSystem(sentences, times); * List<String> param_1 = obj.input(c); */