Skip to content

Commit 6d37427

Browse files
added 0006 Wild Card Matching (L-A)
1 parent 8878a7f commit 6d37427

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 0005 Trapping Rain Water ( L-A )
2+
3+
## Problem
4+
5+
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
6+
7+
## Example 1
8+
9+
```
10+
Input: s = "aa", p = "a"
11+
Output: false
12+
Explanation: "a" does not match the entire string "aa".
13+
```
14+
15+
## Solution
16+
17+
```javascript
18+
function isMatch(s, p) {
19+
const memo = {};
20+
return dp(0, 0, memo);
21+
22+
function dp(i, j, memo) {
23+
const key = `${i}-${j}`;
24+
25+
if (memo.hasOwnProperty(key)) {
26+
return memo[key];
27+
}
28+
29+
if (j === p.length) {
30+
return i === s.length;
31+
}
32+
33+
const firstMatch = i < s.length && (p[j] === s[i] || p[j] === "?");
34+
35+
if (p[j] === "*") {
36+
memo[key] =
37+
dp(i, j + 1, memo) ||
38+
(firstMatch && dp(i + 1, j, memo));
39+
} else {
40+
memo[key] = firstMatch && dp(i + 1, j + 1, memo);
41+
}
42+
43+
return memo[key];
44+
}
45+
}
46+
47+
```
48+
49+
## How it works
50+
- This solution uses dynamic programming with memoization to efficiently solve the problem. The `dp` function takes two indices (`i` and `j`) to represent the current positions in the string `s` and pattern `p`, respectively, as well as a memoization object (`memo`) to store previous results.
51+
- The base cases for the recursion are when the pattern is empty (`j === p.length`) and the string is empty (`i === s.length`). In this case, the function returns `true` if the string is empty as well, and ``false` otherwise.
52+
- If the current character in the pattern is a wildcard (`'*'`), there are two possibilities: either the wildcard matches 0 characters (in which case we move to the next character in the pattern by calling `dp(i, j + 1, memo)`), or the wildcard matches 1 or more characters (in which case we move to the next character in the string by calling `dp(i + 1, j, memo)` if there is a match at the current position). We use the `||` operator to combine these two possibilities, and memoize the result.
53+
- If the current character in the pattern is not a wildcard, we check if there is a match at the current position (`firstMatch = i < s.length && (p[j] === s[i] || p[j] === '?')`). If there is a match, we move to the next character in both the string and the pattern by calling `dp(i + 1, j + 1, memo)`. We memoize the result and return it.
54+
- At the end, we call the `dp` function with the initial indices (`0` and `0`) and the memoization object. The result of the function represents whether the string matches the pattern.
55+
56+
## References
57+
58+
- [LeetCode](https://leetcode.com/problems/wildcard-matching/)
59+
60+
## Problem Added By
61+
62+
- [Haris](https://github.com/harisdev-netizen)
63+
64+
## Contributing
65+
66+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
67+
68+
Please make sure to update tests as appropriate.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function isMatch(s, p) {
2+
const memo = {}; // memoization object to store previous results
3+
return dp(0, 0, memo);
4+
5+
function dp(i, j, memo) {
6+
const key = `${i}-${j}`; // key to store and retrieve memoization results
7+
8+
if (memo.hasOwnProperty(key)) {
9+
// check if result has already been computed
10+
return memo[key];
11+
}
12+
13+
if (j === p.length) {
14+
// if pattern is empty, string must also be empty
15+
return i === s.length;
16+
}
17+
18+
const firstMatch = i < s.length && (p[j] === s[i] || p[j] === "?");
19+
20+
if (p[j] === "*") {
21+
// if current character is wildcard
22+
memo[key] =
23+
dp(i, j + 1, memo) || // match 0 characters
24+
(firstMatch && dp(i + 1, j, memo)); // match 1 or more characters
25+
} else {
26+
// if current character is not a wildcard
27+
memo[key] = firstMatch && dp(i + 1, j + 1, memo); // match current characters
28+
}
29+
30+
return memo[key];
31+
}
32+
}

0 commit comments

Comments
 (0)