-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0044-wildcard-matching.js
50 lines (46 loc) · 1.02 KB
/
0044-wildcard-matching.js
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
/**
* 44. Wildcard Matching
* https://leetcode.com/problems/wildcard-matching/
* Difficulty: Hard
*
* 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).
*/
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
let i = 0;
let j = 0;
let start = -1;
let offset = -1;
while (i < s.length) {
if (j < p.length && s[i] === p[j] || p[j] === '?') {
i++;
j++;
} else if (j < p.length && p[j] === '*') {
start = j;
offset = i;
j++;
} else if (start === -1) {
return false;
} else {
j = start + 1;
i = offset + 1;
offset = i;
}
}
for (let index = j; index < p.length; index++) {
if (p[index] !== '*') {
return false;
}
}
return true;
};