Skip to content

Commit a52f1c9

Browse files
committed
Add solution #44
1 parent d62f834 commit a52f1c9

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

0044-wildcard-matching.js

Lines changed: 50 additions & 0 deletions
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+
};

README.md

Lines changed: 1 addition & 0 deletions
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|

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy