| 7 | * @return {boolean} |
| 8 | */ |
| 9 | var isMatch = (text, pattern) => { |
| 10 | const isBaseCase = pattern.length === 0; |
| 11 | if (isBaseCase) return text.length === 0; |
| 12 | |
| 13 | const isTextAndPatternEqual = pattern[0] === text[0], |
| 14 | isPatternPeriod = pattern[0] === '.', |
| 15 | isFirstMatch = text && (isTextAndPatternEqual || isPatternPeriod), |
| 16 | isNextPatternWildCard = pattern.length >= 2 && pattern[1] === '*'; |
| 17 | |
| 18 | return isNextPatternWildCard /* Time O((N + M) * 2^(N + (M / 2))) | Space O(N^2 + M^2) */ |
| 19 | ? isMatch(text, pattern.slice(2)) || |
| 20 | (isFirstMatch && isMatch(text.slice(1), pattern)) |
| 21 | : isFirstMatch && isMatch(text.slice(1), pattern.slice(1)); |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * DP - Top Down |