MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / isMatch

Function isMatch

javascript/0010-regular-expression-matching.js:9–22  ·  view source on GitHub ↗
(text, pattern)

Source from the content-addressed store, hash-verified

7 * @return {boolean}
8 */
9var 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

Callers 1

checkFunction · 0.70

Calls 4

initMemoFunction · 0.70
checkFunction · 0.70
initTabuFunction · 0.70
searchFunction · 0.70

Tested by

no test coverage detected