Meta / Amazon / Google / Microsoft Interview Question: Wildcard Matching Solution | LeetCode-44: Hard | JavaScript Implementation
2 min readFeb 11, 2022
We will be seeing the solution to the LeetCode problem 44 in JavaScript. I hope you will find it useful.
Problem Statement:
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for ‘?’ and ‘*’ where:
‘?’ Matches any single character.
‘*’ Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
Link to Problem: https://leetcode.com/problems/wildcard-matching/
Implementation:
var isMatch = function (s, p) {
let memo = [];
const util = (i, j) => {
if (i == s.length && j == p.length) {
return true;
}
if (j >= p.length) {
return false;
}
if (i >= s.length) {
if (p[j] == "*") {
return util(i, j + 1);
} else {
return false;
}
}
if (memo[i] != null && memo[i][j] != null) {
return memo[i][j];
}
if (memo[i] == null) {
memo[i] = [];
}
if (memo[i] != null && memo[i][j] == null) {
memo[i][j] = [];
}
if (s[i] == p[j] || p[j] == "?") {
memo[i][j] = util(i + 1, j + 1);
return memo[i][j];
} else if (p[j] == "*") {
memo[i][j] = util(i + 1, j) || util(i, j + 1);
return memo[i][j];
} else {
return false;
}
};
return util(0, 0);
};
Thank you for reading hope this code is helpful to you.