|
| 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. |
0 commit comments