Related: Wildcard Matching, Leetcode
Wildcard Pattern Matching - GeeksforGeeks
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)
Read full article from Wildcard Pattern Matching - GeeksforGeeks
Wildcard Pattern Matching - GeeksforGeeks
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)
Case 1: The character is ‘*’
Here two cases arise
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.
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.
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.
Let T[i][j] is true if first i characters in given string matches the first j characters of pattern.
DP Initialization:
// both text and pattern are null T[0][0] = true; // pattern is null T[i][0] = false; // text is null T[0][j] = T[0][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
bool strmatch(char str[], char pattern[], int n, int m){ // empty pattern can only match with // empty string if (m == 0) return (n == 0); // lookup table for storing results of // subproblems bool lookup[n + 1][m + 1]; // initailze lookup table to false memset(lookup, false, sizeof(lookup)); // empty pattern can match with empty string lookup[0][0] = true; // Only '*' can match with empty string for (int j = 1; j <= m; j++) if (pattern[j - 1] == '*') lookup[0][j] = lookup[0][j - 1]; // fill the table in bottom-up fashion for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; 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[j - 1] == '*') lookup[i][j] = lookup[i][j - 1] || lookup[i - 1][j]; // Current characters are considered as // matching in two cases // (a) current character of pattern is '?' // (b) characters actually match else if (pattern[j - 1] == '?' || str[i - 1] == pattern[j - 1]) lookup[i][j] = lookup[i - 1][j - 1]; // If characters don't match else lookup[i][j] = false; } } return lookup[n][m];}