Wildcard Queries

Hard
0/120
Average time to solve is 50m
profile
Contributed by
10 upvotes
Asked in company
Google inc

Problem statement

You are given a dictionary ‘D’ consisting of ‘N’ words. Each word of the dictionary is of fixed size ‘L’ and contains only lowercase English alphabets.

Now you have to answer ‘Q’ queries, in each query you are given a word ‘W’ of fixed size ‘L’ but this word can consist of either lowercase English alphabets or ‘?’ (wildcard characters). Where ‘?’ can be matched by any lowercase English alphabet.

For each query find how many words in the dictionary can be matched by the query word ‘W’ by replacing ‘?’(if present) with some lowercase English alphabet.

 

Example :
If the dictionary is: {"cat", "rat", "bat", "hat"}

And query words are "?at" and "b?t", then:

"?at" => can be matched by all the 4 dictionary words by replacing '?' with 'c', 'r', 'b' or 'h'.

"b?t" => can only be matched by the dictionary word "bat" by replacing '?' with 'a'.  
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains a single integer ‘T’ denoting the number of test cases. Then each test case follows.

The first line of each test case contains two integers ‘N’ and ‘L’, where N denotes the number of words in the dictionary and L denotes the length of words in the dictionary and queries.

The next line contains N words each of size L, denoting the words in the dictionary.

The next line contains a single integer ‘Q’ denoting the number of queries.

The following Q lines, each contains a word ‘W’ of size L, denoting the query word.
Output Format :
For each test case print Q integers in a separate line, each denoting the number of dictionary words that can be matched by the query word.

Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 10      
1 <= N <= 100
1 <= L <= 7
1 <= Q <= 100
1 <= word.size <= L

Time limit: 1 sec
Sample Input 1 :
2
5 3
cat
map
bat
man
pen
4
?at
ma?
?a?
??n
1 5
aaaaa
1
bbbbb
Sample Output 1 :
2
2
4
2
0
Explanation Of Sample Output 1 :
For test case 1 :
Query1: “?at” matches with “cat” and “bat”
Query2: “ma?” matches with “map” and “man”
Query3: “?a?” matches with “cat”, “bat”, “map” and “man”
Query4: “??n” matches with “man” and “pen”

For test case 2 :
Query1: “bbbbb” doesn’t matches with any dictionary words
Sample Input 2 :
2
5 3
cat
map
bat
man
pen
4
cat
ma?
?a?
pen
2 5
aaaaa
bbbbb
1
bbbbb
Sample Output 2 :
1
2
4
1
1
Hint

Try generating all possible words that can be formed from the word given in the query.

Approaches (3)
Backtracking To Generate All Possible Words

Store the dictionary words in a hash-map for a constant lookup time, now for each query word, try replacing each ‘?’ with every possible lowercase alphabet and generating all possible words that can be formed by the query word, for each of them: search whether it exists in the dictionary or not.

The steps are as follows:

  1. Initialize finalAnswer array to store the answer to each query.
  2. Initialize a hash-map mp. And store all the dictionary words in it.
  3. For each query initialize count = 0 and generate all possible words that can be formed by replacing ‘?’ using backtracking.
  4. For each generated word, increment count if it exists in hash-map mp.
  5. Print the insert the value of count after generating all the words in the finalAnswer array.
  6. Move to the next query and repeat the same thing from 2nd Step.
  7. When all the queries have been processed return the finalAnswer array
Time Complexity

O( Q * L^26 ), where ‘Q’ is the number of queries and ‘L’ is the size of the word.


For each query, in the worst condition the word of size ‘L’ contains only ‘?’, this will result in generating L^26 possible words using backtracking. Hence the time complexity is O(Q * L^26) 

Space Complexity

O(N), where ‘N’ is the size of the dictionary.


A Hash-map of size equal to ‘N’ is created to store the words in the dictionary. Hence the space complexity is O(N).

Code Solution
(100% EXP penalty)
Wildcard Queries
Full screen
Console