var isMatch = function (s, p) {
return helper(s, p);
function helper(str, pattern) {
if (pattern.length === 0) {
if (str.length === 0) {
return true;
} else {
return false;
}
}
// 不是 * 号的情况
if (str.length > 2 && (str[0] === pattern[0] || pattern[0] === '.') && pattern[1] !== '*') {
res = helper(str.slice(1),pattern.slice(1));
} else if (str.length > 0 && (str[0] === pattern[0] || pattern[0] === '.') && pattern[1] === '*'){
// 这是 *号 代表0次的情况 多次的情况
res = helper(str,pattern.slice(2)) || helper(str.slice(1),pattern)
} else {
if (str.length > 0 && (str[0] === pattern[0] || pattern[0] === '.') && pattern[1] !== '*') {
res = helper(str.slice(1),pattern.slice(1));
} else if (pattern[1] === '*') {
res = helper(str,pattern.slice(2))
} else {
return false;
}
}
return res;
}
};
复制代码