-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.js
More file actions
27 lines (24 loc) · 680 Bytes
/
10.js
File metadata and controls
27 lines (24 loc) · 680 Bytes
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
// apporoach: move two cursor through s and p to match them use rules.
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
function __isMatch(i ,j) {
if (j >= p.length) {
return i >= s.length;
}
var firstCharMatch = i < s.length && (s[i] === p[j] || p[j] === '.');
if (p[j + 1] === '*') {
return __isMatch(i, j + 2) || firstCharMatch && __isMatch(i + 1, j); // no match || match one
}
return firstCharMatch && __isMatch(i + 1, j + 1);
}
return __isMatch(0, 0);
};
// js solution
var isMatch = function(s, p) {
var regExp = new RegExp('^' + p + '$');
return regExp.test(s);
};