-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cs
43 lines (40 loc) · 1.22 KB
/
Solution.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
using System.Linq;
public class Solution {
public bool IsMatch(string s, string p) {
if (p.Count(ch => ch != '*') > s.Length)
{
return false;
}
bool[,] f = new bool[s.Length + 1, p.Length + 1];
bool[] d = new bool[s.Length + 1]; // d[i] means f[0, j] || f[1, j] || ... || f[i, j]
for (var j = 0; j <= p.Length; ++j)
{
d[0] = j == 0 ? true : d[0] && p[j - 1] == '*';
for (var i = 0; i <= s.Length; ++i)
{
if (j == 0)
{
f[i, j] = i == 0;
continue;
}
if (p[j - 1] == '*')
{
if (i > 0)
{
d[i] = f[i, j - 1] || d[i - 1];
}
f[i, j] = d[i];
}
else if (p[j - 1] == '?')
{
f[i, j] = i > 0 && f[i - 1, j - 1];
}
else
{
f[i, j] = i > 0 && f[i - 1, j - 1] && s[i - 1] == p[j - 1];
}
}
}
return f[s.Length, p.Length];
}
}