Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65b7854

Browse files
committed
upd: regular dp solution
1 parent 793c84d commit 65b7854

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎src/regularExpressionMatching.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,28 @@ var isMatch = function(s, p) {
1515
return firstMatch && isMatch(s.substr(1), p.substr(1));
1616
}
1717
};
18+
19+
var isMatchDp = function (s, p) {
20+
let temp = [];
21+
let dp = function (i, j) {
22+
if (!(temp[i] && temp[i][j])) {
23+
let ret = false,
24+
firstMatch = false;
25+
temp[i] = [];
26+
if (j === p.length) {
27+
ret = i === s.length;
28+
} else {
29+
firstMatch = i < s.length && !!~['.', s[i]].indexOf(p[j]);
30+
if (j + 1 < p.length && p[j + 1] === '*') {
31+
ret = dp(i, j + 2) || firstMatch && dp(i + 1, j);
32+
} else {
33+
ret = firstMatch && dp(i + 1, j + 1);
34+
}
35+
}
36+
temp[i][j] = ret;
37+
}
38+
return temp[i][j];
39+
};
40+
41+
return dp(0, 0);
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /