Skip to content

Commit 6d2a2d3

Browse files
authored
Create 44-wildcard-matching.js
1 parent 2bcf6d6 commit 6d2a2d3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

44-wildcard-matching.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} p
4+
* @return {boolean}
5+
*/
6+
const isMatch = function(s, p) {
7+
const M = s.length
8+
const N = p.length
9+
let i = 0,
10+
j = 0,
11+
lastMatchInS,
12+
lastStarPos
13+
while (i < M) {
14+
if (j < N && (p[j] === s[i] || p[j] === '?')) {
15+
i++
16+
j++
17+
} else if (j < N && p[j] === '*') {
18+
lastStarPos = j
19+
j++
20+
lastMatchInS = i
21+
} else if (lastStarPos !== undefined) {
22+
// back to previous step
23+
j = lastStarPos + 1
24+
lastMatchInS++
25+
i = lastMatchInS
26+
} else {
27+
return false
28+
}
29+
}
30+
while (j < N && p[j] === '*') {
31+
j++
32+
}
33+
return j === N
34+
}

0 commit comments

Comments
 (0)