


Let’s say you have “abcd” as string ‘ST’ and 2 words “bd” and “db”. “bd” matches with the string ‘ST’ as you can remove ‘a’ and ‘c’ character to form “bd”. “db” does not match with the string ‘ST’ because it is impossible to form “db” from the string ‘ST’ by removing characters.
You cannot change the order of characters in ‘ST’ after removing some characters.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two single space-separated integers ‘N’ and ‘X’ representing the length of string ‘ST’ and number of words respectively.
The second line contains the string ‘ST’.
Each of the next ‘X’ lines contains ‘X’ words.
For each test case, return the largest matching word among these ‘X’ words. If there are multiple strings of largest length return lexicographically smallest string. If no word matches from ‘ST’ return ‘#’ as the string.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 100
1 <= X <= 100
Time Limit: 1 sec
We will first find all the words that can be formed by removing some characters from ‘ST’ and then for each of 'X' words we will check if it can be formed from ‘ST’.
We maintain an unordered set( hashmap ) that stores all the words that can be formed from 'S'. We write a recursive function ‘FORM_WORD(int idx, string S, string W)’ to find all the words that can be formed from ‘ST’:-
For each word:-
Following is the algorithm for this approach, for each word: