Skip to content

Commit d0b15f3

Browse files
committed
Add solution #44
1 parent 4f8f54a commit d0b15f3

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
41|[First Missing Positive](./0041-first-missing-positive.js)|Hard|
5050
42|[Trapping Rain Water](./0042-trapping-rain-water.js)|Hard|
5151
43|[Multiply Strings](./0043-multiply-strings.js)|Medium|
52+
44|[Wildcard Matching](./0044-wildcard-matching.js)|Hard|
5253
45|[Jump Game II](./0045-jump-game-ii.js)|Medium|
5354
46|[Permutations](./0046-permutations.js)|Medium|
5455
47|[Permutations II](./0047-permutations-ii.js)|Medium|

solutions/0044-wildcard-matching.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* 44. Wildcard Matching
3+
* https://leetcode.com/problems/wildcard-matching/
4+
* Difficulty: Hard
5+
*
6+
* Given an input string (s) and a pattern (p), implement wildcard pattern
7+
* matching with support for '?' and '*' where:
8+
*
9+
* - '?' Matches any single character.
10+
* - '*' Matches any sequence of characters (including the empty sequence).
11+
*
12+
* The matching should cover the entire input string (not partial).
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @param {string} p
18+
* @return {boolean}
19+
*/
20+
var isMatch = function(s, p) {
21+
let i = 0;
22+
let j = 0;
23+
let start = -1;
24+
let offset = -1;
25+
26+
while (i < s.length) {
27+
if (j < p.length && s[i] === p[j] || p[j] === '?') {
28+
i++;
29+
j++;
30+
} else if (j < p.length && p[j] === '*') {
31+
start = j;
32+
offset = i;
33+
j++;
34+
} else if (start === -1) {
35+
return false;
36+
} else {
37+
j = start + 1;
38+
i = offset + 1;
39+
offset = i;
40+
}
41+
}
42+
43+
for (let index = j; index < p.length; index++) {
44+
if (p[index] !== '*') {
45+
return false;
46+
}
47+
}
48+
49+
return true;
50+
};

0 commit comments

Comments
 (0)