


1) The suggested correct strings for the string ‘QUERY’ will be all those strings present in the ‘DICTIONARY[]’ that have the prefix same as the longest prefix of string ‘QUERY’.
2) The ‘DICTIONARY[]’ contains only distinct strings.
Given 'DICTIONARY[]' = {“ninja”, “ninjas”, “nineteen”, “ninny”} and query = “ninjk”. Since “ninjk” is not present in the ‘DICTIONARY[]’, it is an incorrect string. The suggested current spellings for “ninjk” are “ninja” and “ninjas”. It is because “ninj” is the longest prefix of “ninjk” which is present as the prefix in ‘DICTIONARY’.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains an integer ‘N’ representing the number of strings in the list, ‘DICTIONARY[].’
The second line of each test case contains ‘N’ space separated strings present in the list, ‘DICTIONARY[]’.
The last line of each test case contains the ‘QUERY’ string.
For each test case, return a list of strings containing all suggested correct spellings from the list, ‘DICTIONARY[]’, if the spelling of the ‘query’ string is incorrect. Otherwise, return an empty list that will be internally interpreted as the correct string and will print “CORRECT”.
The output of each test case will be printed 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 <= 5
1 <= N <= 100
1 <= |DICTIONARY[i]| <= 100
1 <= |QUERY| <= 100
‘DICTIONARY[i]’ and ‘QUERY’ contains only lowercase english letters.
Where ‘|DICTIONARY[i]|’ is the length of the string in the list ‘DICTIONARY' and ‘|QUERY|’ is the length of the ‘QUERY’ string.
Time limit: 1 sec
The entire problem is reduced to a prefix matching problem when the ‘QUERY' string is not present in the ‘DICTIONARY’. The idea is to use trie data structure as it is the most suitable and fastest data structure in prefix matching.
First, we will build a trie and insert all the strings of ‘DICTIONARY’ in the trie. Then, we will traverse the string, ‘QUERY and match it with the character present in the trie. At any moment, the character does not match, the substring before that character will be the longest prefix of string, ‘QUERY. So, we will store all the strings from the ‘DICTIONARY’ that matches with the longest prefix of string, ‘QUERY, in a list and then return the list.
If the string, ‘QUERY matches completely with any string in the trie. It suggests that ‘QUERY is spelt correctly. So, we will return an empty list.
Structure of ‘Trie’:
The steps are as follows:
Description of ‘INSERT’ function:
This function will take two parameters :
void insert(‘ROOT’, ‘STR’):
Description of ‘findSuggestions’ function:
This function will take three parameters :
void findSuggestions(‘ROOT’, ‘possibleAnswer', ‘RES’):