


The ‘DICTIONARY[]’ contains only distinct strings.
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 third line of each test case contains an integer ‘Q’ representing the number of queries (or characters in the stream).
The last line of each test case contains ‘Q’ space-separated characters.
For each query in a test case, return true if the string formed by the last ‘C’ characters (C >= 1) is present in the ‘DICTIONARY[]’. Otherwise, return false.
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 <= 200
1 <= |DICTIONARY[i]| <= 200
1 <= Q <= 400
‘DICTIONARY[i]’ and ‘QUERY’ contains only lowercase english letters.
Time limit: 1 second
The idea is to use the brute force approach for this problem. At every queried character, we will form strings in a backward direction and will simultaneously check for the string in the ‘DICTIONARY’. If we found the string, then return true. Otherwise, check till the first queried character. If still no string found in ‘DICTIONARY’, return false.
Note:
Algorithm:
It is not easier to develop a solution to this problem by the usual approach. But if we look at the problem in a way to match the reverse of ‘C’ stream of characters with reversed strings present in the ‘dictionary’. Then the entire problem is reduced to a prefix matching problem. The idea is to use trie data structure as it is the most suitable and fastest data structure for prefix matching.
First, we will build a trie and insert all the strings of ‘DICTIONARY’ in reversed order in the ‘TRIE’. We will maintain a string, ‘STREAM’, that will store the stream of characters at any instant. For every queried character, search the ‘STREAM’ from the end in the ‘TRIE’. If we found a node representing the end of the word, it means that character present at that node till the queried character is a valid word present in the ‘DICTIONARY’. So, we will return true.
If not, then we have two cases:
‘Trie’ Class:
Algorithm:
Description of ‘insert’ function
This function will take one parameter :
STR: A string variable to store the string from ‘DICTIONARY’.
void insert(str):
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
Design Search Autocomplete System
Design Search Autocomplete System
Design Search Autocomplete System
Design Search Autocomplete System
Design Search Autocomplete System
Design Search Autocomplete System