-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathWildCardMatching.cpp
More file actions
149 lines (127 loc) · 3.92 KB
/
WildCardMatching.cpp
File metadata and controls
149 lines (127 loc) · 3.92 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// https://leetcode.com/problems/wildcard-matching/description/
class Solution {
public:
// Simple Recursive Solution
// bool solve(string& s, string& p, int i, int j){
// if(i<0 && j<0)
// return true;
// if(i>=0 && j<0)
// return false;
// if(i<0 && j>=0){
// for(int k=0; k<=j; k++){
// if(p[k]!='*'){
// return false;
// }
// }
// return true;
// }
// //match
// if(s[i]==p[j] || p[j]=='?'){
// return solve(s, p, i-1, j-1);
// }
// else if(p[j]=='*'){
// return (solve(s, p, i-1, j) || solve(s, p, i, j-1));
// }
// else
// return false;
// }
// Recursion + Memoization (Top-Down Approach)
// bool solve(string& s, string& p, int i, int j, vector<vector<int>>& dp){
// if(i==0 && j==0)
// return true;
// if(i>0 && j==0)
// return false;
// if(i==0 && j>0){
// for(int k=1; k<=j; k++){
// if(p[k-1]!='*'){
// return false;
// }
// }
// return true;
// }
// if(dp[i][j]!=-1)
// return dp[i][j];
// //match
// bool ans;
// if(s[i-1]==p[j-1] || p[j-1]=='?'){
// ans= solve(s, p, i-1, j-1, dp);
// }
// else if(p[j-1]=='*'){
// ans= (solve(s, p, i-1, j, dp) || solve(s, p, i, j-1, dp));
// }
// else
// ans= false;
// dp[i][j]= ans;
// return dp[i][j];
// }
// Tabulation Method (Bottom-Up Approach)
// bool solve(string& s, string& p){
// vector<vector<int>> dp(s.length()+1, vector<int>(p.length()+1, 0));
// dp[0][0]= true;
// for(int j=1; j<=p.length(); j++){
// bool flag= true;
// for(int k=1; k<=j; k++){
// if(p[k-1]!='*'){
// flag= false;
// break;
// }
// }
// dp[0][j]= flag;
// }
// for(int i=1; i<=s.length(); i++){
// for(int j=1; j<=p.length(); j++){
// //match
// bool ans;
// if(s[i-1]==p[j-1] || p[j-1]=='?'){
// ans= dp[i-1][j-1];
// }
// else if(p[j-1]=='*'){
// ans= (dp[i-1][j] || dp[i][j-1]);
// }
// else
// ans= false;
// dp[i][j]= ans;
// }
// }
// return dp[s.length()][p.length()];
// }
bool solve(string& s, string& p){
vector<int> prev(p.length()+1, 0);
vector<int> curr(p.length()+1, 0);
prev[0]= true;
for(int j=1; j<=p.length(); j++){
bool flag= true;
for(int k=1; k<=j; k++){
if(p[k-1]!='*'){
flag= false;
break;
}
}
prev[j]= flag;
}
for(int i=1; i<=s.length(); i++){
for(int j=1; j<=p.length(); j++){
//match
bool ans;
if(s[i-1]==p[j-1] || p[j-1]=='?'){
ans= prev[j-1];
}
else if(p[j-1]=='*'){
ans= (prev[j] || curr[j-1]);
}
else
ans= false;
curr[j]= ans;
}
prev= curr;
}
return prev[p.length()];
}
bool isMatch(string s, string p) {
// Simple Recursive Solution
// return solve(s, p, s.length()-1, p.length()-1);
// vector<vector<int>> dp(s.length()+1, vector<int>(p.length()+1, -1));
// return solve(s, p, s.length(), p.length(), dp);
return solve(s, p);
}
};