Design a search autocomplete system for a search engine. Users may input a sentence (at least one word and end with a special character'#'). For each character they type except '#', you need to return the top 3 historical hot sentences that have prefix the same as the part of 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 degree of hot, you need to use ASCII-code order (smaller one appears first).
If less than 3 hot sentences exist, then just 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.
Your job is to implement the following functions:
The constructor function:
AutocompleteSystem(String[] sentences, int[] times):This is the constructor. The input ishistorical data.Sentencesis a string array consists of previously typed sentences.Timesis the corresponding times a sentence has been typed. Your system should record these historical data.
Now, the user wants to input a new sentence. The following function will provide the next character the user types:
List<String> input(char c):The inputcis the next character typed by the user. The character will only be lower-case letters ('a'to'z'), blank space (' ') or a special character ('#'). Also, the previously typed sentence should be recorded in your system. The output will be thetop 3historical hot sentences that have prefix the same as the part of sentence already typed.
Example:Operation:AutocompleteSystem(["i love you", "island","ironman", "i love leetcode"], [5,3,2,2])
The system have already tracked down the following sentences and their corresponding times:
"i love you":5times
"island":3times
"ironman":2times
"i love leetcode":2times
Now, the user begins another search:
Operation:input('i')
Output:["i love you", "island","i love leetcode"]
Explanation:
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.
Operation:input(' ')
Output:["i love you","i love leetcode"]
Explanation:
There are only two sentences that have prefix"i ".
Operation:input('a')
Output:[]
Explanation:
There are no sentences that have prefix"i a".
Operation:input('#')
Output:[]
Explanation:
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.
Note:
The input sentence will always start with a letter and end with '#', and only one blank space will exist between two words.
The number of
complete sentences
that to be searched won't exceed 100. The length of each sentence including those in the historical data won't exceed 100.
Please use double-quote instead of single-quote when you write test cases even for a character input.
Approach 1: Tries
The Idea: I am assuming prerequisite knowledge of the trie data structure. The reason that it makes sense to apply in our context is because an autocomplete system is based on the idea that as a user types, all what is produced from the system is a list of words that all share common prefixes. In a trie, all root to leaf paths will represent all the words that share a common prefix with the root. So in our algorithm, this means we first need to build the common prefix by entering the next character in the trie, and then we can DFS the tree to find all the possible options words that share this prefix. Then we can sort this list and return the top 3 elements that is sorted first by the frequency first in descending order, and the by the word itself in ascending order to break ties.
Complexity: O(n + nlogn) time where n is the number of common prefixes with the accumulated input string.
from copy import copyclassAutocompleteSystem:def__init__(self,sentences,times):""" :type sentences: List[str] :type times: List[int] """ self.trie = AutocompleteSystem.make_trie(sentences, times) self.trie_iter = self.trie self.string_builder = []definput(self,c):""" :type c: str :rtype: List[str] """# idea: find all root to leaf paths in trie,# then just top 3 most frequent ones rtlp = []defdfs(root,cur_word):""" :param cur_word: word that is currently being built up :return: None """for c, next_trie in root.items():if c !='#': cur_word.append(c)dfs(next_trie, cur_word) cur_word.pop()else: rtlp.append((''.join(cur_word), root['#']))# first go to the next character from the current rootif c !='#':# if the character doesnt exist within the trie, we are at the start# of a new branch (new input) in the trieif c notin self.trie_iter: self.trie_iter[c]={} self.trie_iter = self.trie_iter[c]return []# otherwise step into the trie rootelse: self.trie_iter = self.trie_iter[c] self.string_builder.append(c)dfs(self.trie_iter, copy(self.string_builder))return [sentence for sentence, _ insorted(rtlp, key=lambdatup: (-tup[1], tup[0]))][:min(3, len(rtlp))]# otherwise reset iter for the next set of input words# and aggregate the input within the trieelse:# this means that the input already exists within the trieif'#'in self.trie_iter: self.trie_iter['#']+=1# otherwise the input is entirely unique within the treeelse: self.trie_iter['#']=1# reset attributes for next input self.trie_iter = self.trie self.string_builder = []return []@staticmethoddefmake_trie(words,times):""" :param words: List[str] :param times: int :return: trie """ trie ={}for word, time inzip(words, times): iter_trie = triefor letter in word:if letter notin iter_trie: iter_trie[letter]={} iter_trie = iter_trie[letter]# assuming the number of occupancies are unique# this should work, otherwise we would have to init# with 'times' and then aggregate forwards iter_trie['#']= timereturn trie
Approach 2: Tries + PriorityQueue
The Idea: The approach here is effectively the same idea, but instead of using a list as our container of the output for the AC system, we substitute it with a priority queue. The reason we do this is because it is not necessary to maintain all possible sentences. Our problem statement tells us only to maintain 3.
Complexity: O(n + nlog3) time where n is the number of common prefixes with the accumulated input string. Worst case example: lets we have n sentences in our prefix tree. Each of these sentences will be put into the priority queue which maintains 3 elements. Each heap operation takes log(3) time, and it is called n amount of times.
from copy import copyfrom queue import PriorityQueueclassAutocompleteSystem:def__init__(self,sentences,times):""" :type sentences: List[str] :type times: List[int] """ self.trie = AutocompleteSystem.make_trie(sentences, times) self.trie_iter = self.trie self.string_builder = []definput(self,c):""" :type c: str :rtype: List[str] """# idea: find all root to leaf paths in trie,# then just top 3 most frequent ones rtlp_pq =PriorityQueue()defdfs(root,cur_word):""" :param cur_word: word that is currently being built up :return: None """for c, next_trie in root.items():if c !='#': cur_word.append(c)dfs(next_trie, cur_word) cur_word.pop()else: my_word =''.join(cur_word)# we need at most 3 elements, maintain a maximum of 3 elements in pqif rtlp_pq.qsize()<3: rtlp_pq.put((-root['#'], my_word))# replace only if the root element is has smaller frequency or in the the# case that they have the same frequency, then order by the word itselfelif (rtlp_pq.qsize()>=3and rtlp_pq.queue[0][0] > root['#']) or\ (rtlp_pq.queue[0][0] == root['#']and rtlp_pq.queue[0][1] > my_word): rtlp_pq.get() rtlp_pq.put((-root['#'], my_word))# first go to the next character from the current rootif c !='#':# if the character doesnt exist within the trie, we are at the start# of a new branch (new input) in the trieif c notin self.trie_iter: self.trie_iter[c]={} self.trie_iter = self.trie_iter[c]return []# otherwise step into the trie rootelse: self.trie_iter = self.trie_iter[c] self.string_builder.append(c)dfs(self.trie_iter, copy(self.string_builder)) top3 = []whilenot rtlp_pq.empty(): top3.append(rtlp_pq.get()[1])return top3# otherwise reset iter for the next set of input words# and aggregate the input within the trieelse:# this means that the input already exists within the trieif'#'in self.trie_iter: self.trie_iter['#']+=1# otherwise the input is entirely unique within the treeelse: self.trie_iter['#']=1# reset attributes for next input self.trie_iter = self.trie self.string_builder = []return []@staticmethoddefmake_trie(words,times):""" :param words: List[str] :param times: int :return: trie """ trie ={}for word, time inzip(words, times): iter_trie = triefor letter in word:if letter notin iter_trie: iter_trie[letter]={} iter_trie = iter_trie[letter]# assuming the number of occupancies are unique# this should work, otherwise we would have to init# with 'times' and then aggregate forwards iter_trie['#']= timereturn trie
Sanity Checks
# obj = AutocompleteSystem(["i love you", "island","ironman", "i love leetcode"], [5,3,2,2])# print(obj.input('i'))# print(obj.input(' '))# print(obj.input('a'))# print(obj.input('#'))obj =AutocompleteSystem(["i love you","island","iroman","i love leetcode"],[5,3,2,2])print(obj.input('i'))print(obj.input(' '))print(obj.input('a'))print(obj.input('#'))obj =AutocompleteSystem(["abc","abbc","a"],[3,3,3])print(obj.input('b'))print(obj.input('c'))print(obj.input('#'))print(obj.input('b'))print(obj.input('c'))print(obj.input('#'))print(obj.input('a'))