-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wildcard Pattern Matching.cpp
126 lines (98 loc) · 3.33 KB
/
Wildcard Pattern Matching.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Problem Link: https://practice.geeksforgeeks.org/problems/wildcard-pattern-matching/1
*/
// -----Approach 1: Memoization ------------------------------------------------------------
class Solution{
private:
int sol(int i, int j, vector<vector<int>> &dp, string pattern, string str){
if(i==0 && j==0) return 1;
if(i==0 && j>0) return 0;
if(j==0 && i>0) {
for(int ch=1; ch<=i; ch++){
if(pattern[ch-1] != '*')
return 0;
return 1;
}
}
if(dp[i][j] != -1) return dp[i][j];
if(pattern[i-1] == str[j-1] || pattern[i-1] == '?') return dp[i][j]= sol(i-1, j-1, dp, pattern, str);
if(pattern[i-1] == '*'){
return dp[i][j]= sol(i, j-1, dp, pattern, str) | sol(i-1, j, dp, pattern, str);
}
else return dp[i][j]= 0;
}
public:
/*You are required to complete this method*/
int wildCard(string pattern, string str)
{
int n= pattern.length();
int m= str.length();
vector<vector<int>> dp(n+1, vector<int>(m+1, -1));
return sol(n, m, dp, pattern, str);
}
};
// -----Approach 2: Tabulation ------------------------------------------------------------
class Solution{
public:
/*You are required to complete this method*/
int wildCard(string pattern, string str)
{
int n= pattern.size();
int m= str.size();
vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
dp[0][0]= 1;
for (int j=1; j<=m; j++) dp[0][j]= 0;
for (int i=1; i<=n; i++){
bool flag = 1;
for(int ch=1; ch<=i; ch++){
if(pattern[ch-1] != '*'){
flag= 0;
break;
}
}
dp[i][0]= flag;
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (pattern[i - 1] == str[j - 1] || pattern[i - 1] == '?')
dp[i][j]= dp[i - 1][j - 1];
else if(pattern[i - 1] == '*') dp[i][j]= dp[i - 1][j] | dp[i][j - 1];
else dp[i][j]= 0;
}
}
return dp[n][m];
}
};
// -----Approach 3: Space Optimization ------------------------------------------------------------
class Solution{
public:
/*You are required to complete this method*/
int wildCard(string pattern, string str)
{
int n= pattern.size();
int m= str.size();
// Space optimization: very tough to implement, use dry run
vector<int> prev(m+1, 0), curr(m+1, 0);
prev[0]= 1;
for (int j=1; j<=m; j++) prev[j]= 0;
for (int i=1; i<=n; i++) {
bool flag = 1;
for(int ch=1; ch<=i; ch++){
if(pattern[ch-1] != '*'){
flag= 0;
break;
}
}
curr[0]= flag;
// inner for loop for string matching
for (int j=1; j<=m; j++) {
if (pattern[i - 1] == str[j - 1] || pattern[i - 1] == '?')
curr[j]= prev[j - 1];
else if(pattern[i - 1] == '*') curr[j]= prev[j] | curr[j - 1];
else curr[j]= 0;
}
prev= curr;
}
return prev[m];
}
};