Ninja has enrolled in a system design course at Coding Ninjas, and his first assignment is to create a search autocomplete system for a search engine.
Ninja is given ‘N’ sentences ‘SENTENCES’ and ‘N’ integers ‘TIMES’, where ‘TIMES’[i] is the number of times the ‘SENTENCES’[i] was typed. Now, the instructor will input a test sentence(at least one word and end with ‘#’). As Ninja’s best friend, can you help him with the assignment?
Your task is to implement the AutocompleteSystemclass:
Here are the specific rules:
For Example:
Input:
["AutocompleteSystem","input","input","input"]
[[["i want to join faang","icecream","i love coding ninjas"],[4,3,1],["i"],["s"],["#"]]
Output:
[null,["i want to join faang","icecream","i love coding ninjas"],[],[]]
Explanation:
* AutocompleteSystem obj = new AutocompleteSystem(["i want to join faang","icecream","i love coding ninjas"],[4,3,1]);
* obj.input(“i”); // return ["i want to join faang","icecream","i love coding ninjas"]. There are three sentences that have prefix "I".
* obj.input(“s”); // return []. There is no sentence with prefix “is”
* obj.input(“#”); // return [].
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line contains an integer ‘N’, representing the number of sentences.
The next line of each test case contains ‘N’ + 1 string where the first string is always “AutocompleteSystem” and the following ‘N’ strings are “input”.
The next ‘N’ lines contain a String, representing a sentence.
The next line contains ‘N’ space-separated integers representing the elements of the ‘TIMES’ array.
The next line contains a sentence inputted by the user for the testing purpose.
Output Format:
For each test case, print all pairs of the distinct indices (i, j) in ‘WORDS’, such that the concatenation of WORDS[i] and WORDS[j] is a palindrome.
Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 100
1 <= SENTENCES[i].LENGTH <= 100
1 <= TIMES[i] <= 50
SENTENCES[i] consists of lowercase English letters
‘C’ is a lowercase English letter, a hash '#', or space ' '.
Each tested sentence will be a sequence of characters c that end with the character '#'.
Each tested sentence will have a length in the range [1, 200].
The words in each input sentence are separated by single spaces.
At most 5000 calls will be made to input.
Time Limit: 1 sec
2
3
i want to join faang
icecream
i love coding ninjas
4 3 1
is#
3
batman
superman
spiderman
4 3 2
sp#
[i want to join faang, icecream, i love coding ninjas]
[]
[]
[superman, spiderman]
[spiderman]
[]
For the first test case,
AutocompleteSystem obj = new AutocompleteSystem(["i want to join faang","icecream","i love coding ninjas"],[4,3,1]);
obj.input(“i”); // return ["i want to join faang","icecream","i love coding ninjas"]. There are three sentences that have prefix "i".
obj.input(“s”); // return []. There is no sentence with prefix “is”
obj.input(“#”); // return [].
For the second test case,
AutocompleteSystem obj = new AutocompleteSystem([["batman","superman","spiderman"],[4,3,1]],["s"],["p"],["#"]]);
obj.input(“s”); // return [superman, spiderman]. There are two sentences that have prefix "s".
obj.input(“p”); // return [spiderman]. There is one sentence with prefix “sp”
obj.input(“#”); // return [].
2
2
stack and queues
trees and grpahs
3 1
t#
4
aaa
aab
abb
aac
3 2 2 1
abc#
[trees and graphs]
[]
[aaa, aab, abb]
[aab]
[]
[]
Use a data structure to store the sentences efficiently.
Prerequisite: Trie data structure
In this approach, we will use a Trie. Trie is an efficient data retrieval data structure mostly used for string manipulations. It provides a way to store strings efficiently and also to search for them in a lot lesser time complexity. We will store the top 3 hot sentences directly on each node. When we get the input character, we will simply read off the answer from the node. Whenever we add a sentence at each node of the corresponding trie, we will update that node’s info appropriately if its top three answer changes. We will use a list to store our answers, which will keep track of only the top three hot sentences.
Each Trie Node will have four fields:
Comparing two Trie Nodes:
A Trie Node ‘A’ is hotter than Trie Node ‘B’ if A.TIMES is greater than B.TIMES. If A.TIMES is equal to B.TIMES, then ‘A’ is hotter than ‘B’ if A.S is lexicographically smaller than B.S. Otherwise, ‘B’ is hotter and will be placed ahead of ‘A’.
How to get the top 3 hot sentences for any testing sentence?
Suppose if the testing sentence is “abc” and there exits a Trie Node ‘T’, where T.s = “abc” then the top 3 hot sentences for this query will be the sentences stored in T.hot.
Algorithm :
Function AUTOCOMPLETESYSTEM(‘SENTENCES’, ‘TIMES’);
Function INPUT(‘C’);
Maintain a class TrieNode:
Maintain a function ADD(‘SENTENCE’, ‘T’):
O(N * L + C), where 'N' is the number of sentences, ‘L’ is the length of the longest sentence, and ‘C’ is the number of characters in the testing sentence.
The time for building Trie is O(N * L), and the time for answering queries is O(C). Hence, the total time complexity is O(N * K).
O(N * L), where 'N' is the number of sentences and ‘L’ is the length of the longest sentence.
We may have to store ‘N’ sentences of length ‘L’ in Trie. Hence, the total space complexity is O(N * L).