Problem of the day
Given a text and a wildcard pattern of size N and M respectively, implement a wildcard pattern matching algorithm that finds if the wildcard pattern is matched with the text. The matching should cover the entire text not partial text.
The wildcard pattern can include the characters ‘?’ and ‘*’
‘?’ – matches any single character
‘*’ – Matches any sequence of characters(sequence can be of length 0 or more)
The first line contains an Integer 'T' which denotes the number of test cases/queries to be run.
Then the test cases follow.
The first line of input for each test case/query contains a string representing the wildcard pattern.
The second line of input for each test case/query contains a string representing the text.
Output Format:
For each test case, print ‘True’ if the text matches the pattern, print ‘False’ otherwise(without quotes).
Output for every 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 function.
1 <= T <= 100
1 <= N <= 200
1 <= M <= 200
Where 'N' denotes the length of 'TEXT' and 'M' denotes the length of 'PATTERN'.
'TEXT' and 'PATTERN' contain only lowercase letters and patterns may contain special characters ‘*’ and ‘?’
Time Limit: 1sec
3
?ay
ray
ab*cd
abdefcd
ab?d
abcc
True
True
False
Test Case 1:
The pattern is “?ay” and the text is ray.
‘?’ can match a character so it matches with a character ‘r’ of the text and rest of the text matches with the pattern so we print True.
Test Case 2:
“ab” of text matches with “ab” of pattern and then ‘*’ of pattern matches with “def” of the text and “cd” of text matches with “cd” of the pattern. Whole text matches with the pattern so we print True.
Test Case 3:
“ab” of pattern matches with “ab” of text. ‘?’ of pattern matches with ‘c’ of the text but ‘d’ of the pattern do not match with ‘c’ of the text so we print False.
1
ba*a?
baaabab
True
Try to explore all the possibilities using the recursion.
We will try to explore all the possibilities using the recursion.
Let’s say we have a recursive function ‘REC’ who has two arguments IDX and IDY where IDX represents the index in the text string and IDY represents the index in pattern string and its return type is boolean. Initially, we will call the rec with IDX = N and IDY = M where N is the size of the text string and M is the size of the pattern string.
We have taken care of the base cases now we need recursive relation to explore the cases. Create a variable RET = 0 to store the answer.
O(2^(N*M), where N is the size of the text and M is the size of the pattern string
Here, in each recursive call to the function we are making two more separate calls based on the character choices ‘?’ and ‘*’ therefore we are populating two more calls from every single call in the search space due to which the complexity here grows by the order of O(2^(N*M)).
O(N*M), where N is the size of the text and M is the size of the pattern string.
Since here at worst case, our recursion will need O(N*M) stack space for the recursive calls that we are making based on the character choices ‘?’ and ‘*’ therefore we are populating two more calls from every single call in the search space due to which the space complexity here is of the order of O(N*M).