-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathWildcard Matching.cpp
39 lines (34 loc) · 1.02 KB
/
Wildcard Matching.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
/*
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).
Example 1:
Input: s = "aa", p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
*/
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.length(), n = p.length();
int i = 0, j = 0, asterisk = -1, match;
while (i < m) {
if (j < n && p[j] == '*') {
match = i;
asterisk = j++;
}
else if (j < n && (s[i] == p[j] || p[j] == '?')) {
i++;
j++;
}
else if (asterisk >= 0) {
i = ++match;
j = asterisk + 1;
}
else return false;
}
while (j < n && p[j] == '*') j++;
return j == n;
}
};