Skip to content

Commit 9dd31c0

Browse files
authored
Create WildCardPatternMatching.cpp
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)
1 parent 2cc5f0e commit 9dd31c0

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// C++ program to implement wildcard
2+
// pattern matching algorithm
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
// Function that matches input str with
7+
// given wildcard pattern
8+
bool strmatch(char str[], char pattern[],
9+
int n, int m)
10+
{
11+
// empty pattern can only match with
12+
// empty string
13+
if (m == 0)
14+
return (n == 0);
15+
16+
// lookup table for storing results of
17+
// subproblems
18+
bool lookup[n + 1][m + 1];
19+
20+
// initailze lookup table to false
21+
memset(lookup, false, sizeof(lookup));
22+
23+
// empty pattern can match with empty string
24+
lookup[0][0] = true;
25+
26+
// Only '*' can match with empty string
27+
for (int j = 1; j <= m; j++)
28+
if (pattern[j - 1] == '*')
29+
lookup[0][j] = lookup[0][j - 1];
30+
31+
// fill the table in bottom-up fashion
32+
for (int i = 1; i <= n; i++)
33+
{
34+
for (int j = 1; j <= m; j++)
35+
{
36+
// Two cases if we see a '*'
37+
// a) We ignore ‘*’ character and move
38+
// to next character in the pattern,
39+
// i.e., ‘*’ indicates an empty sequence.
40+
// b) '*' character matches with ith
41+
// character in input
42+
if (pattern[j - 1] == '*')
43+
lookup[i][j] = lookup[i][j - 1] ||
44+
lookup[i - 1][j];
45+
46+
// Current characters are considered as
47+
// matching in two cases
48+
// (a) current character of pattern is '?'
49+
// (b) characters actually match
50+
else if (pattern[j - 1] == '?' ||
51+
str[i - 1] == pattern[j - 1])
52+
lookup[i][j] = lookup[i - 1][j - 1];
53+
54+
// If characters don't match
55+
else lookup[i][j] = false;
56+
}
57+
}
58+
59+
return lookup[n][m];
60+
}
61+
62+
int main()
63+
{
64+
char str[] = "baaabab";
65+
char pattern[] = "*****ba*****ab";
66+
// char pattern[] = "ba*****ab";
67+
// char pattern[] = "ba*ab";
68+
// char pattern[] = "a*ab";
69+
// char pattern[] = "a*****ab";
70+
// char pattern[] = "*a*****ab";
71+
// char pattern[] = "ba*ab****";
72+
// char pattern[] = "****";
73+
// char pattern[] = "*";
74+
// char pattern[] = "aa?ab";
75+
// char pattern[] = "b*b";
76+
// char pattern[] = "a*a";
77+
// char pattern[] = "baaabab";
78+
// char pattern[] = "?baaabab";
79+
// char pattern[] = "*baaaba*";
80+
81+
if (strmatch(str, pattern, strlen(str),
82+
strlen(pattern)))
83+
cout << "Yes" << endl;
84+
else
85+
cout << "No" << endl;
86+
87+
return 0;
88+
}

0 commit comments

Comments
 (0)