


The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘3*T’ lines represent the ‘T’ test cases.
The first line of each test case contains the string ‘S’.
The second line of each test contains an integer ‘N’ representing the number of words in the ‘wordList’.
The third line of each test case contains the ‘N’ space-separated strings representing the words in the ‘wordList’.
For each test case, print a boolean array where the value at index ‘i’ is ‘True’ if the word at index ‘i’ in the ‘wordList’ is present in string ‘S’ Otherwise, it is ‘False’.
1. String ‘S’ consists of lowercase, uppercase alphabets, and spaces.
2. String ‘Wi’ consists of lowercase and uppercase alphabets only.
3. The word, ‘Wi’ is case sensitive.
4. You can’t use language-built-in string-matching methods.
5. Don’t print anything, just return the boolean array determining the presence or absence of the words present in ‘wordList’.
1 <= T <= 50
1 <= |S| <= 10^3
1 <= N <= 10^3
1 <= |W| <= 10
Where ‘|S|’ denotes the length of string and |W| denotes the maximum length of the word present in ‘wordList’.
Time limit: 1 sec
KMP algorithm or Rabin-Karp algorithm can search for a substring in a string in linear time. We can use it to optimize the run time of our solution.
1 If the length of ‘Wi’ is greater than ‘S’, then it cannot be a substring of ‘S’.
2. Otherwise, use either KMP or Rabin-Karp to check whether the ‘Wi’ is a substring of ‘S’ or not.
3. If ‘Wi’ is a substring of ‘S’. then set result[i] = true.
Trie, also known as ‘prefix tree’ is a tree-like data structure where nodes of the tree store entire alphabets and strings can be retrieved by traversing down a branch part of the tree. Trie allows us to search for any key in O(key_length) time.
Here, we can build a trie containing all the words in ‘wordList’ and then iterate through string ‘S’ and check whether any part of string ’S’ is present in the trie or not as mentioned below.