You are given a list, ‘DICTIONARY[]’ containing a list of words and a stream of characters (queries). Your task is to choose a suitable data structure to implement ‘CharacterStreamChecker’ class described as follows:
1) ‘CharacterStreamChecker(dictionary)’: Constructor to initialize the data structure with given words present in ‘DICTIONARY[]’.
2) ‘solveQuery(character)’: Function to check whether the string formed by last ‘C’ (C >= 1) queried characters (in order from oldest to newest, including the character just queried) is present in the ‘DICTIONARY[]’. If yes, return true. Otherwise, return false.
Note :
The ‘DICTIONARY[]’ contains only distinct strings.
Input Format :
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.
Output Format :
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.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
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
2
4
i am a ninja
9
a a m n i n j a m
3
mn p qrs
7
m n o p q r s
true true true false true false false true true
false true false true false false true
Test Case 1 :
Stream: ‘a’. String “a” is present in the ‘DICTIONARY’. Therefore, the output is true.
Stream: ‘a’, ‘a’. String “a” is present in the ‘DICTIONARY’. Therefore, the output is true.
Stream: ‘a’, ‘a’, ‘m’. String “am” is present in the ‘DICTIONARY’. Therefore, the output is true.
Stream: ‘a’, ‘a’, ‘m’, ‘n’. No string out of “n”, “mn”, “amn” and “aamn” is present in the ‘DICTIONARY’. Therefore, the output is false.
Stream: ‘a’, ‘a’, ‘m’, ‘n’, ‘i’. String “i” is present in the ‘DICTIONARY’. Therefore, the output is true.
Stream: ‘a’, ‘a’, ‘m’, ‘n’, ‘i’, ‘n’. No string out of “n”, “in”, “nin”, “mnin”, “amnin” and “aamnin” is present in the ‘DICTIONARY’. Therefore, the output is false.
Stream: ‘a’, ‘a’, ‘m’, ‘n’, ‘i’, ‘n’, ‘j’. No string out of “j”, “nj”, “inj”, “ninj”, “mninj”, “amninj” and “aamninj” is present in the ‘DICTIONARY’. Therefore, the output is false.
Stream: ‘a’, ‘a’, ‘m’, ‘n’, ‘i’, ‘n’, ‘j’, ‘a’. String “ninja” is present in the ‘DICTIONARY’. Therefore, the output is true.
Stream: ‘a’, ‘a’, ‘m’, ‘n’, ‘i’, ‘n’, ‘j’, ‘a’, ‘m’. String “am” is present in the ‘DICTIONARY’. Therefore, the output is true.
Test Case 2 :
Stream: ‘m’. String “m” is not present in the ‘DICTIONARY’. Therefore, the output is false.
Stream: ‘m’, ‘n’. String “mn” is present in the ‘DICTIONARY’. Therefore, the output is true.
Stream: ‘m’, ‘n’, ‘o’. No string out of “o”, “no”, and “mno” is present in the ‘DICTIONARY’. Therefore, the output is false.
Stream: ‘m’, ‘n’, ‘o’, ‘p’. String “p” is present in the ‘DICTIONARY’. Therefore, the output is true.
Stream: ‘m’, ‘n’, ‘o’, ‘p’, ‘q’. No string out of “q”, “pq”, “opq”, “nopq” and “mnopq” is present in the ‘DICTIONARY’. Therefore, the output is false.
Stream: ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’. No string out of “r”, “qr”, “pqr”, “opqr”, “nopqr” and “mnopqr” is present in the ‘DICTIONARY’. Therefore, the output is false.
Stream: ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’. String “qrs” is present in the ‘DICTIONARY’. Therefore, the output is true.
2
5
mnn nn nnnn nmmmm nmn
7
n n n m n m n
4
a aa ab abc
5
c a b c a
false true true false true false true
false true true true true
Use brute force to check for every string formed from the current character (in the backward direction) is present in the dictionary or not.
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:
O(N + (Q * (M ^ 2))), where ‘N’ is the number of strings in the ’DICTIONARY’, ‘Q’ is the number of queries, and ‘M’ is the maximum length of the string present in the ‘DICTIONARY’.
The time complexity of the ‘CharacterStreamChecker’ constructor is O(N). Since the loop inside the constructor runs ‘N’ times to insert the string in ‘HASHSET’ (Insertion in a hash set is O(1)).
Time Complexity of ‘solveQuery’ function is O((M ^ 2)) because in the worst case, the loop inside the ‘solveQuery’ function run ‘M’ times wherein each iteration of the loop, inserting the character at the beginning of the ‘temp’ string takes O(M) time. Because of ‘Q’ queries, it will result in O(Q * (M ^ 2)).
Hence, overall time complexity is O(N) + O(Q * (M ^ 2)) = O(N + (Q * (M ^ 2))).
O(N + Q + M), where ‘N’ is the number of strings in the ’dictionary’, ‘Q’ is the number of queries, and ‘M’ is the maximum length of the string present in the ‘dictionary’.
Since the ‘HASHSET’ requires O(N) space to store the strings present in the ‘DICTIONARY’, string, ‘STREAM’ will store ‘Q’ characters at the end resulting in O(Q) space and string ‘TEMP’ will store a maximum of ‘M’ characters at any instant that will result in O(M) space.