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)
'+' – Matches previous single character
of pattern
Examples:
Input :Text = "baaabaaa", Pattern = “****+ba*****a+", output : true Pattern = "baaa?ab", output : false Pattern = "ba*a?", output : true Pattern = "+a*ab", output : false Input : Text = "aab" Pattern = "*+" output : false Pattern = "*+b" output : true
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 ‘+’
Here two cases arise : We match current character of text with the previous character of pattern. If there is no previous character that mean ‘+’ is the first character of pattern so we print result as “text do not match”. If the previous character is either ‘+’, ‘?’ or ‘*’ we replace it with the last character used by them.
Case 4: 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.
The process for “*”, “?” is similar to wildcard pattern Matching for two characters:
Here we use a dp table that will contain
two fields
Struct DP
{
// value is true if match possible
// for current indexes, else false.
bool value;
// Stores the character used
// by the symbol that we used
// later for symbol '+'
char ch;
}
Below c++ implementation of above idea.
// C++ program to implement wildcard // pattern matching algorithm #include <bits/stdc++.h> using namespace std; struct DP { bool value; char ch; }; // Function that matches input str with // given wildcard pattern bool strmatch(string str, string pattern, int n, int m) { // empty pattern can only match with // empty string if (m == 0) return (n == 0); // If first character of pattern is '+' if (pattern[0] == '+') return false; // lookup table for storing results of // subproblems struct DP lookup[m + 1][n + 1]; // initialize lookup table to false for (int i = 0; i <= m; i++) for (int j = 0; j <= n; j++) { lookup[i][j].value = false; lookup[i][j].ch = ' '; } // empty pattern can match with // empty string lookup[0][0].value = true; // Only '*' can match with empty string for (int j = 1; j <= n; j++) if (pattern[j - 1] == '*') lookup[j][0].value = lookup[j - 1][0].value; // fill the table in bottom-up fashion for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { // Two cases if we see a '*' // 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 if (pattern[i - 1] == '*') { lookup[i][j].value = lookup[i][j - 1].value || lookup[i - 1][j].value; lookup[i][j].ch = str[j - 1]; } // Current characters are considered as // matching in two cases // (a) current character of pattern is '?' else if (pattern[i - 1] == '?') { lookup[i][j].value = lookup[i - 1][j - 1].value; lookup[i][j].ch = str[j - 1]; } // (b) characters actually match else if (str[j - 1] == pattern[i - 1]) lookup[i][j].value = lookup[i - 1][j - 1].value; // Current character match else if (pattern[i - 1] == '+') // case 1: if previous character is // not symbol if (pattern[i - 2] != '+' || pattern[i - 2] != '*' || pattern[i - 2] != '?') if (pattern[i - 2] == str[j - 1]) { lookup[i][j].value = lookup[i - 1][j - 1].value; lookup[i][j].ch = str[j - 1]; } // case 2 : if previous character // is symbol (+, *, ? ) then we // compare current text character // with the character that is used by // the symbol at that point. we // access it by lookup[i-1][j-1] else if (str[j-1] == lookup[i-1][j-1].ch) { lookup[i][j].value = lookup[i - 1][j - 1].value; lookup[i][j].ch = lookup[i - 1][j - 1].ch; } // If characters don't match else lookup[i][j].value = false; } } return lookup[m][n].value; } // Driver code int main() { string str = "baaabaaa"; string pattern = "*****+ba***+"; if (strmatch(str, pattern, str.length(), pattern.length())) cout << "Yes" << endl; else cout << "No" << endl; return 0; } |
Output:
Yes
Time Complexity : O(n*m)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Wildcard Pattern Matching
- Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space
- String matching where one string contains wildcard characters
- Print all words matching a pattern in CamelCase Notation Dictonary
- CamelCase Pattern Matching
- Split numeric, alphabetic and special symbols from a String
- Parsing String of symbols to Expression
- How to find index of any Currency Symbols in a given string
- Count ways to split a Binary String into three substrings having equal count of zeros
- Program to print a doormat pattern having a string written in the center in Python
- Longest Common Prefix using Word by Word Matching
- Longest Common Prefix using Character by Character Matching
- Prefix matching in Python using pytrie module
- Longest Common Prefix Matching | Set-6
- String matching with * (that matches with any) in any of the two strings
- Count the Number of matching characters in a pair of strings
- Applications of String Matching Algorithms
- Find minimum sum such that one of every three consecutive elements is taken
- Maximum subsequence sum such that no three are consecutive
- LCS (Longest Common Subsequence) of three strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

