forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0010-regular-expression-matching.js
98 lines (81 loc) · 3.7 KB
/
0010-regular-expression-matching.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Brute Force - DFS
* Time O((N + M) * 2^(N + (M / 2))) | Space O(N^2 + M^2)
* https://leetcode.com/problems/regular-expression-matching/
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = (text, pattern) => {
const isBaseCase = (pattern.length === 0);
if (isBaseCase) return (text.length === 0);
const isTextAndPatternEqual = (pattern[0] === text[0]),
isPatternPeriod = (pattern[0] === '.'),
isFirstMatch = (text && (isTextAndPatternEqual || isPatternPeriod)),
isNextPatternWildCard = (pattern.length >= 2 && pattern[1] === '*');
return isNextPatternWildCard/* Time O((N + M) * 2^(N + (M / 2))) | Space O(N^2 + M^2) */
? (isMatch(text, pattern.slice(2)) || (isFirstMatch && isMatch(text.slice(1), pattern)))
: (isFirstMatch && isMatch(text.slice(1), pattern.slice(1)));
};
/**
* DP - Top Down
* Matrix - Memoization
* Time O(N * M) | Space O(N * M)
* https://leetcode.com/problems/regular-expression-matching/
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = (text, pattern, row = 0, col = 0, memo = initMemo(text, pattern)) => {
const hasSeen = (memo[row][col]);
if (hasSeen) return memo[row][col];
const isEqual = (col === pattern.length);
const ans = isEqual
? row === text.length
: check(text, pattern, row, col, memo);/* Time O(N * M) | Space O(N * M) */
memo[row][col] = ans;
return ans;
}
var initMemo = (text, pattern) => new Array((text.length + 1)).fill()/* Time O(N) | Space O(N) */
.map(() => new Array((pattern.length + 1)).fill(false)) /* Time O(M) | Space O(M) */
var check = (text, pattern, row, col, memo) => {
const isTextDefined = (row < text.length),
isTextAndPatternEqual = (pattern[col] === text[row]),
isPatternPeriod = (pattern[col] === '.'),
isFirstMatch = (isTextDefined && (isTextAndPatternEqual || isPatternPeriod)),
isNextPatternWildCard = (((col + 1) < pattern.length) && pattern[col + 1] === '*');
return isNextPatternWildCard/* Time O(N * M) | Space O(N * M) */
? (isMatch(text, pattern, row, (col + 2), memo) || isFirstMatch && isMatch(text, pattern, (row + 1), col, memo))
: (isFirstMatch && isMatch(text, pattern, (row + 1), (col + 1), memo));
}
/**
* Time O(N * M) | Space O(N * M)
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = (text, pattern) => {
const tabu = initTabu(text, pattern);/* Time O(N * M) | Space O(N * M) */
search(text, pattern, tabu); /* Time O(N * M) | Space O(N * M) */
return tabu[0][0];
}
var initTabu = (text, pattern) => {
const tabu = new Array((text.length + 1)).fill() /* Time O(N) | Space O(N) */
.map(() => new Array((pattern.length + 1)).fill(false));/* Time O(M) | Space O(M) */
tabu[text.length][pattern.length] = true; /* | Space O(N * M) */
return tabu
}
var search = (text, pattern, tabu) => {
for (let row = text.length; 0 <= row; row--){ /* Time O(N) */
for (let col = (pattern.length - 1); (0 <= col); col--){/* Time O(M) */
const isTextDefined = row < text.length,
isTextAndPatternEqual = pattern[col] === text[row],
isPatternPeriod = pattern[col] === '.',
isFirstMatch = isTextDefined && (isTextAndPatternEqual || isPatternPeriod),
isNextPatternWildCard = col + 1 < pattern.length && pattern[col + 1] === '*';
tabu[row][col] = isNextPatternWildCard /* Space O(N * M) */
? tabu[row][col + 2] || (isFirstMatch && tabu[row + 1][col])
: isFirstMatch && tabu[row + 1][col + 1];
}
}
}