


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.
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.
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
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’):
Maximum Distance
Maximum Distance
Maximum Distance
Maximum Distance
Maximum Distance
Complete String
Complete String
Complete String
Complete String
Complete String
Complete String
Similar Name
Similar Name
Similar Name
Similar Name
Auto Suggestion
Auto Suggestion
Auto Suggestion
Auto Suggestion
Auto Suggestion
Auto Suggestion
Palindrome Pairs