-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.js
44 lines (40 loc) · 1.14 KB
/
Solution.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
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function (s, p) {
return isMatchFromIndex(s, 0, p, 0);
};
var isMatchFromIndex = function (s, i, p, j) {
if (j == p.length) {
return i == s.length;
}
var curP = p.charAt(j);
if (j + 1 < p.length && p.charAt(j + 1) == '*') {
while (i < s.length && (curP == '.' || s.charAt(i) == curP)) {
if (isMatchFromIndex(s, i, p, j + 2)) {
return true;
}
i++;
}
return isMatchFromIndex(s, i, p, j + 2);
} else {
if (curP == '.' || s.charAt(i) == curP) {
return isMatchFromIndex(s, i + 1, p, j + 1);
} else {
return false;
}
}
};
function test() {
console.log(isMatch("aa", "a") == false);
console.log(isMatch("aa", "aa") == true);
console.log(isMatch("aaa", "aa") == false);
console.log(isMatch("aa", "a*") == true);
console.log(isMatch("aa", ".*") == true);
console.log(isMatch("ab", ".*") == true);
console.log(isMatch("aab", "c*a*b") == true);
console.log(isMatch("bbbba", ".*a*a") == true);
}
test();