You are given a list/array of strings which denotes the contacts that exist in your phone directory. The search query on a string ‘str’ which is a query string displays all the contacts which are present in the given directory with the prefix as ‘str’. One special property of the search function is that when a user searches for a contact from the contact list then suggestions (contacts with prefix as the string entered so for) are shown after the user enters each character.
Note :If no suggestions are found, return an empty 2D array.
For Example :

In the above example everytime we enter a character, a few suggestions display the strings which contain the entered string as prefixes.
The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of elements in the array/list.
The second line contains ‘N’ single space-separated strings denoting the contacts of the given array/list or contact list.
The third line contains a single string denoting the query string.
Output format :
For each test case, for the given query string you need to print all the possible contacts or suggestions that are present in the given directory corresponding to each entered character in the given query string.
If no suggestions are found then the message “No suggestions found” is printed.
Note :
You do not need to print anything; It has already been taken care of.
1 <= T <= 50
1 <= N <= 100
1 <= len <= 10
ARR[i] contains lowercase English alphabets.
All the given strings contain lowercase English alphabets.
Time Limit: 1 sec.
2
5
cod coding codding code coly
coding
2
ninjas coding
ninja
cod coding codding code coly
cod coding codding code coly
cod coding codding code coly
coding
coding
coding
ninjas
ninjas
ninjas
ninjas
ninjas
In the first test case,
The suggestions for “c” is {“cod”, “coding”, “codding”, “code”, “coly”}.
The suggestions for “co” is {“cod”, “coding”, “codding”, “code”, “coly”}.
The suggestions for “cod” is {“cod”, “coding”, “codding”, “code”, “coly”}.
The suggestion for “codi” is {“coding”}.
The suggestion for “codin” is {“coding”}.
The suggestion for “coding” is {“coding”}.
In the second test case,
The suggestion for “n” is {“ninjas”}.
The suggestion for “ni” is {“ninjas”}.
The suggestion for “nin” is {“ninjas”}.
The suggestion for “ninj” is {“ninjas”}.
The suggestion for “ninja” is {“ninjas”}.
3
2
coding ninjas
cell
2
ab abc
a
2
ab abc
b
coding
ab abc
No suggestions found
In the first test case,
The suggestion for “c” is {“coding”}.
For the rest of the letters, there are no suggestions.
In the second test case,
The suggestion for “a” is {“ab”, “abc”}.
In the third test case, no suggestions are found.
Think of a data structure that can be used to search a string.
A search query on a Trie is being used to determine whether the string is present or not in the trie, but in this case, we are asked to find all the strings with each prefix of ‘str’. This is equivalent to doing a DFS traversal on a graph.
From a Trie node, we need to visit adjacent Trie nodes and do this recursively until there is no more adjacent. This recursive function will take 2 arguments one as Trie Node which points to the current Trie Node being visited and the other as the string which stores the string found so far with prefix as ‘str’. We are creating an unordered_map where each alphabet points to a trie node.
Each Trie Node stores a boolean variable “isEnd” which becomes true if the node represents the end of contact or element of the given contact list. It is nothing but a variable that signifies that we don’t need to move forward to do a search because we reached the end of the particular word of the contactList.
O(N * (W ^ 2)), where ‘N’ is the number of elements in the given array/list and ‘W’ is the average length of the string.
Consider an example when our word list will be like this :
{“a”,”aa”,”aaa”,”aaaa”,”aaaaa”,”aaaaaa”,”aaaaaa”} and we are looking for suggestions for the query string “aaaaaa”.
So for inserting the given contact list into trie will take O(N*W) time. And secondly, in order to store the suggestions, we’ll include all the lists for every character of the given prefix.
For example, Now considering all the W characters in the prefix and traversing all the N words which will take O(N*W), and combining both of these will result in O(W*(N*W)) time. So the overall time complexity will be O(N*(W^2)).
O(N * W), where ‘N’ is the number of elements in the given array/list and ‘W’ is the average length of the string.
Since for each character in the prefix of the intermediate strings we are storing a map for its children where each alphabet points to a trie node resulting in taking the space for ‘W’ characters as O(N * W) space since we are looking for each string in the given word list.
So the overall space complexity will be O(N * W).