-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0044.wildcard_matching_H.cpp
43 lines (42 loc) · 1.19 KB
/
0044.wildcard_matching_H.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
/**
* https://leetcode.com/problems/wildcard-matching/
* 2019/08
* 104 ms
*/
class Solution
{
public:
bool isMatch(string s, string p)
{
s = "x" + s;
p = "x" + p;
int Ns = s.length();
int Np = p.length();
vector<vector<bool>> M(Ns, vector<bool>(Np, false));
queue<pair<int, int>> Q;
M[0][0] = true;
Q.push(pair<int, int>(0, 0));
while(! Q.empty())
{
int is = Q.front().first;
int ip = Q.front().second;
Q.pop();
if('*' == p[ip] && is + 1 < Ns && ! M[is + 1][ip])
{
M[is + 1][ip] = true;
Q.push(pair<int, int>(is + 1, ip));
}
if(ip + 1 < Np && '*' == p[ip + 1] && ! M[is][ip + 1])
{
M[is][ip + 1] = true;
Q.push(pair<int, int>(is, ip + 1));
}
if(is + 1 < Ns && ip + 1 < Np && ('?' == p[ip + 1] || s[is + 1] == p[ip + 1]) && ! M[is + 1][ip + 1])
{
M[is + 1][ip + 1] = true;
Q.push(pair<int, int>(is + 1, ip + 1));
}
}
return M[s.length() - 1][p.length() - 1];
}
};