动态规划求解:
// @lc code=start
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
let row = s.length;
let col = p.length;
let dp = new Array(row + 1);
for (let i = 0; i <= row; i++) {
dp[i] = new Array(col + 1).fill(0);
}
dp[0][0] = 1;
for (let i = 1; i <= col; i++) {
if (p[i - 1] == '*') {
dp[0][i] = 1;
} else {
break;
}
}
for (let i = 1; i <= row; i++) {
for (let j = 1; j <= col; j++) {
const val = p[j - 1];
if (val == '*') {
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
} else if (val == '?' || val == s[i - 1]) {
dp[i][j] = dp[i - 1][j - 1];
}
}
}
return Boolean(dp[row][col]);
};
console.log(isMatch('adceb', '*a*b')); // true
// @lc code=end
感谢观看 期待关注
关注我,带你一起写bug
warning :未经授权,不得转载
有问题的小伙伴请在下方留言,喜欢就点个赞吧