Meta / Amazon / Google / Microsoft Interview Question: Wildcard Matching Solution | LeetCode-44: Hard | JavaScript Implementation

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);
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store