-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwildcardMatching.cpp
More file actions
35 lines (32 loc) · 948 Bytes
/
wildcardMatching.cpp
File metadata and controls
35 lines (32 loc) · 948 Bytes
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
// Source: https://leetcode.com/problems/wildcard-matching/
// Author: Miao Zhang
// Date: 2021-01-12
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size();
int n = p.size();
bool dp[m + 1][n + 1];
int i, j;
for (i = 0; i < m + 1; i++) {
for (j = 0; j < n + 1; j++) {
dp[i][j] = false;
}
}
dp[0][0] = true;
for (j = 1; j < n + 1; j++) {
dp[0][j] = dp[0][j - 1] and (p[j - 1] == '*');
}
for (i = 1; i < m + 1; i++) {
for (j = 1; j < n + 1; j++) {
if (p[j - 1] == '*') {
dp[i][j] = dp[i - 1][j] or dp[i][j - 1];
}
else if(s[i - 1] == p[j - 1] or p[j - 1] == '?') {
dp[i][j] = dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
};