-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0044-WildcardMatching.cs
320 lines (294 loc) · 9.28 KB
/
0044-WildcardMatching.cs
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using Xunit;
using Util;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
namespace WildcardMatching
{
public class Solution
{
public bool IsMatch(string s, string p)
{
if (s.Length == 0)
{
for (var i = 0; i < p.Length; i++)
{
if (p[i] != '*')
{
return false;
}
}
// if patt are *, then Match is true
return true;
}
if (p.Length == 0)
{
if (s.Length == 0)
{
return true;
}
else
{
return false;
}
}
var pList = new List<string>();
int idx = 0;
var cs = "";
while (true)
{
if (idx >= p.Length)
{
// add last cs
if ((p[idx - 1] != '?') && p[idx - 1] != '*')
{
pList.Add(cs);
}
break;
}
if ((p[idx] == '?') || p[idx] == '*')
{
if (cs.Length > 0)
{
pList.Add(cs);
cs = "";
}
if ((p[idx] == '*') && (pList.Count > 0) && (pList.Last() == "*"))
{
// duplicated *
}
else
{
pList.Add($"{p[idx]}");
}
}
else
{
cs = $"{cs}{p[idx]}";
}
idx++;
}
// Console.WriteLine(string.Join(", ", pList));
if (pList.Count > 0)
{
var last = pList.Last();
if ((last != "?") && (last != "*"))
{
idx = s.Length - last.Length;
if ((idx >= 0) && (s.Substring(idx, last.Length) != last))
{
// last patt not match
return false;
}
}
}
// check all patts(except *) can be found in s in order
idx = 0;
for (var pi = 0; pi < pList.Count; pi++)
{
if (idx >= s.Length)
{
for (var j = pi; j < pList.Count; j++)
{
if (pList[j] != "*")
{
// s reaches end, but remain patts are not *, so not match
return false;
}
}
break;
}
var ps = pList[pi];
if (ps == "?")
{
idx++;
}
else if (ps != "*")
{
var x = s.IndexOf(ps, idx);
if (x < 0)
{
// patt not found
return false;
}
idx = x + ps.Length;
}
}
if ((pList.Count > 1) && !pList.Contains("?") && (pList.First() == "*") && (pList.Last() == "*"))
{
// *a*b*c*, if a,b,c are in order(checked above), then match is true
return true;
}
// easy way end, try hard way
return Match(s, pList, 0, 0);
}
bool Match(string s, List<string> pList, int si, int pi)
{
while (true)
{
if (pi >= pList.Count)
{
if (si < s.Length)
{
return false;
}
else
{
return true;
}
}
if (si >= s.Length)
{
if (pi < pList.Count)
{
for (var i = pi; i < pList.Count; i++)
{
// if remain patt are *, then Match is true
if (pList[i] != "*")
{
return false;
}
}
return true;
}
else
{
return true;
}
}
var ps = pList[pi];
if ((ps != "?") && (ps != "*"))
{
var subs = new string(s.Skip(si).Take(ps.Length).ToArray());
// Console.WriteLine(subs);
if (subs != ps)
{
return false;
}
pi++;
si += ps.Length;
continue;
}
if (ps == "?")
{
pi++;
si++;
continue;
}
if (ps == "*")
{
while (si < s.Length)
{
// try all possible *
var m = Match(s, pList, si, pi + 1);
if (m)
{
return true;
}
si++;
}
pi++;
continue;
}
}
}
}
public class Test
{
static void Verify(string s, string p, bool exp)
{
Console.WriteLine($"{s}=>/{p}/");
bool res;
using (new Timeit())
{
res = new Solution().IsMatch(s, p);
}
Assert.Equal(exp, res);
}
static public void Run()
{
Console.WriteLine("WildcardMatching");
string s, p;
bool exp;
s = "aa";
p = "a";
exp = false;
Verify(s, p, exp);
s = "aa";
p = "*";
exp = true;
Verify(s, p, exp);
s = "cb";
p = "?a";
exp = false;
Verify(s, p, exp);
s = "adceb";
p = "*a*b";
exp = true;
Verify(s, p, exp);
s = "acdcb";
p = "a*c?b";
exp = false;
Verify(s, p, exp);
s = "aaabbbaabaaaaababaabaaabbabbbbbbbbaabababbabbbaaaaba";
p = "a*******b";
exp = false;
Verify(s, p, exp);
s = "";
p = "a";
exp = false;
Verify(s, p, exp);
s = "b";
p = "?";
exp = true;
Verify(s, p, exp);
s = "bbbaaabaababbabbbaabababbbabababaabbaababbbabbbabb";
p = "*b**b***baba***aaa*b***";
exp = false;
Verify(s, p, exp);
s = "a";
p = "";
exp = false;
Verify(s, p, exp);
s = "abbabaaabbabbaababbabbbbbabbbabbbabaaaaababababbbabababaabbababaabbbbbbaaaabababbbaabbbbaabbbbababababbaabbaababaabbbababababbbbaaabbbbbabaaaabbababbbbaababaabbababbbbbababbbabaaaaaaaabbbbbaabaaababaaaabb";
p = "**aa*****ba*a*bb**aa*ab****a*aaaaaa***a*aaaa**bbabb*b*b**aaaaaaaaa*a********ba*bbb***a*ba*bb*bb**a*b*bb";
exp = false;
Verify(s, p, exp);
s = "baaaa";
p = "*aaa";
exp = true;
Verify(s, p, exp);
s = "aaaaaabbaabaaaaabababbabbaababbaabaababaaaaabaaaabaaaabababbbabbbbaabbababbbbababbaaababbbabbbaaaaaaabbaabbbbababbabbaaabababaaaabaaabaaabbbbbabaaabbbaabbbbbbbaabaaababaaaababbbbbaabaaabbabaabbaabbaaaaba";
p = "*bbb**a*******abb*b**a**bbbbaab*b*aaba*a*b**a*abb*aa****b*bb**abbbb*b**bbbabaa*b**ba**a**ba**b*a*a**aaa";
exp = false;
Verify(s, p, exp);
s = "a";
p = "aa";
exp = false;
Verify(s, p, exp);
s = "aaaabaabaabbbabaabaabbbbaabaaabaaabbabbbaaabbbbbbabababbaabbabbbbaababaaabbbababbbaabbbaabbaaabbbaabbbbbaaaabaaabaabbabbbaabababbaabbbabababbaabaaababbbbbabaababbbabbabaaaaaababbbbaabbbbaaababbbbaabbbbb";
p = "**a*b*b**b*b****bb******b***babaab*ba*a*aaa***baa****b***bbbb*bbaa*a***a*a*****a*b*a*a**ba***aa*a**a*";
exp = false;
Verify(s, p, exp);
s = "baaaba";
p = "a*b??a";
exp = false;
Verify(s, p, exp);
s = "zacabz";
p = "*a?b*";
exp = false;
Verify(s, p, exp);
s = "baaab";
p = "*ab**ba*";
exp = false;
Verify(s, p, exp);
s = "missingtest";
p = "mi*ing?s*t";
exp = false;
Verify(s, p, exp);
}
}
}