-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path37_WildCardMatching.cpp
209 lines (182 loc) · 5.13 KB
/
37_WildCardMatching.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// https://leetcode.com/problems/wildcard-matching/
// Given an input string (s) and a pattern (p), implement wildcard pattern matching
// with support for '?' and '*' where:
// '?' Matches any single character.
// '*' Matches any sequence of characters (including the empty sequence).
// The matching should cover the entire input string (not partial).
#include <bits/stdc++.h>
using namespace std;
class soluiton4
{
// Tabulation: Wrong solution
public:
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
vector<vector<bool>> dp(m+1, vector<bool>(n+1, false));
dp[0][0] = true;
for (int i = 0; i < n; ++i)
{
if (p[i] == '*') dp[0][i+1] = true;
else break;
}
for (int i = 1; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (s[i] == p[j] || p[j] == '?') dp[i][j] = dp[i-1][j-1];
else if (p[j] == '*') dp[i][j] = (dp[i-1][j] || dp[i][j-1]);
else dp[i][j] = false;
}
}
return dp[m][n];
}
};
class soluiton3
{
// Recursion: Memoization
public:
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
vector<vector<int>> dp(m, vector<int>(n, -1));
return solve(s, p, dp, m-1, n-1);
}
bool solve(string &s, string &p, vector<vector<int>> &dp, int i, int j) {
if (j == -1) return i == -1;
if (i == -1)
{
while (j > -1)
{
if (p[j] != '*'){return false;}
--j;
}
return true;
}
if (dp[i][j] != -1) return dp[i][j];
if (s[i] == p[j] || p[j] == '?') return dp[i][j] = solve(s, p, dp, i - 1, j - 1);
if (p[j] == '*') return dp[i][j] = (solve(s, p, dp, i-1, j) || solve (s, p, dp, i, j-1));
return dp[i][j] = false;
}
};
class soluiton2
{
// Recursion
public:
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
return solve(s, p, m-1, n-1);
}
bool solve(string &s, string &p, int i, int j) {
if (j == -1) return i == -1;
if (i == -1)
{
while (j > -1)
{
if (p[j] != '*'){return false;}
--j;
}
return true;
}
if (s[i] == p[j] || p[j] == '?') return solve(s, p, i - 1, j - 1);
if (p[j] == '*') return (solve(s, p, i-1, j) || solve (s, p, i, j-1));
return false;
}
};
class Solution1
{
// Tabulation : Wrong Solution
public:
bool isMatch(string s, string p)
{
int m = s.length(), n = p.length();
vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
dp[0][0] = true;
for (int i = 1; i <= p.length(); ++i)
{
if (p[i - 1] == '*')
{
dp[0][i] = true;
}
else
{
break;
}
}
for (int i = 1; i < s.length(); ++i)
{
for (int j = 1; j < p.length(); ++j)
{
if (s[i - 1] == p[j - 1] || p[j] == '?')
dp[i][j] = dp[i - 1][j - 1];
else if (p[j] == '*')
for (int k = 0; k <= i; ++k)
{
if (dp[i - k][j - 1])
dp[i][j] = true;
}
else
dp[i][j] = false;
}
}
return dp[m][n];
}
};
class Solution1
{
// Recursion: Memoization
public:
bool isMatch(string s, string p)
{
int m = s.length(), n = p.length();
vector<vector<int>> dp(m, vector<int>(n, -1));
return solve(s, p, dp, m - 1, n - 1);
}
bool solve(string &s, string &p, vector<vector<int>> &dp, int i, int j)
{
if (j == -1) return i == -1;
if (i == -1)
{
while (j > -1)
{
if (p[j] != '*'){return false;}
--j;
}
return true;
}
if (dp[i][j] != -1) return dp[i][j];
if (s[i] == p[j] || p[j] == '?') return dp[i][j] = solve(s, p, dp, i - 1, j - 1);
if (p[j] == '*') for (int k = 0; k <= i + 1; ++k) {if (solve(s, p, dp, i - k, j - 1)) return dp[i][j] = true;}
return dp[i][j] = false;
}
};
class Solution
{
// BruteForce: Recursion
public:
bool isMatch(string s, string p)
{
int m = s.length(), n = p.length();
return solve(s, p, m - 1, n - 1);
}
bool solve(string &s, string &p, int i, int j)
{
if (j == -1)
return i == -1;
if (i == -1)
return j == -1;
if (s[i] == p[j] || p[j] == '?')
return solve(s, p, i - 1, j - 1);
if (p[j] == '*')
for (int k = 0; k <= i + 1; ++k)
{
if (solve(s, p, i - k, j - 1))
return true;
}
return false;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}