Given a text and a wildcard pattern, implement wildcard pattern matching algorithm that finds if wildcard pattern is matched with 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 (including the empty sequence)
Text = "baaabab", Pattern = “*****ba*****ab", output : true Pattern = "baaa?ab", output : true Pattern = "ba*a?", output : true Pattern = "a*ab", output : false
Each occurrence of ‘?’ character in wildcard pattern can be replaced with any other character and each occurrence of ‘*’ with a sequence of characters such that the wildcard pattern becomes identical to the input string after replacement.
Let’s consider any character in the pattern.
Case 1: The character is ‘*’
Here two cases arise
- We can ignore ‘*’ character and move to next character in the Pattern.
- ‘*’ character matches with one or more characters in Text. Here we will move to next character in the string.
Case 2: The character is ‘?’
We can ignore current character in Text and move to next character in the Pattern and Text.
Case 3: The character is not a wildcard character
If current character in Text matches with current character in Pattern, we move to next character in the Pattern and Text. If they do not match, wildcard pattern and Text do not match.
We can use Dynamic Programming to solve this problem –
Let T[i][j] is true if first i characters in given string matches the first j characters of pattern.
// both text and pattern are null T = true; // pattern is null T[i] = false; // text is null T[j] = T[j - 1] if pattern[j – 1] is '*'
DP relation :
// If current characters match, result is same as // result for lengths minus one. Characters match // in two cases: // a) If pattern character is '?' then it matches // with any character of text. // b) If current characters in both match if ( pattern[j – 1] == ‘?’) || (pattern[j – 1] == text[i - 1]) T[i][j] = T[i-1][j-1] // If we encounter ‘*’, two choices are possible- // a) We ignore ‘*’ character and move to next // character in the pattern, i.e., ‘*’ // indicates an empty sequence. // b) '*' character matches with ith character in // input else if (pattern[j – 1] == ‘*’) T[i][j] = T[i][j-1] || T[i-1][j] else // if (pattern[j – 1] != text[i - 1]) T[i][j] = false
Below is the implementation of above Dynamic Programming approach.
Time complexity of above solution is O(m x n). Auxiliary space used is also O(m x n).
We can improve space complexity by making use of the fact that we only uses the result from last row.
One more improvement is yo merge consecutive ‘*’ in the pattern to single ‘*’ as they mean the same thing. For example for pattern “*****ba*****ab”, if we merge consecutive stars, the resultant string will be “*ba*ab”. So, value of m is reduced from 14 to 6.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- WildCard pattern matching having three symbols ( * , + , ? )
- Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space
- String matching where one string contains wildcard characters
- CamelCase Pattern Matching
- Print all words matching a pattern in CamelCase Notation Dictonary
- Longest Common Prefix Matching | Set-6
- String matching with * (that matches with any) in any of the two strings
- Prefix matching in Python using pytrie module
- Count the Number of matching characters in a pair of strings
- Longest Common Prefix using Character by Character Matching
- Longest Common Prefix using Word by Word Matching
- Pattern Searching using C++ library
- Print a pattern without using any loop
- Mountain Sequence Pattern
- KMP Algorithm for Pattern Searching
- Check if a string follows a^nb^n pattern or not
- Substring Reverse Pattern
- Count strings that end with the given pattern
- Program for Reversed String Pattern
- Program to print Spiral Pattern