-
Notifications
You must be signed in to change notification settings - Fork 1
/
011_simple_regex.js
71 lines (54 loc) · 1.72 KB
/
011_simple_regex.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
// http://careercup.com/question?id=5124942133723136
// Write a function to check if a string matches a regex patter.
// Note that you only have to deal with patterns containing ""*"".
// Also, note that the pattern can''t start with ""*"".
// Some examples:
// isMatch('aa','a') → false
// isMatch('aa','aa') → true
// isMatch('aaa','aa') → false
// isMatch('aa', 'a*') → true
// isMatch('a', 'b*a') → true
// isMatch('a', 'a*a') → true
// isMatch('aab', 'c*a*b') → true
function isMatch(s, r) {
var p = [];
var i = 0;
while (i < r.length) {
var pp = { ch: r[i], cmd: '' };
i++;
if (i < r.length && r[i] == '*') {
pp.cmd = '*';
i++;
}
p.push(pp);
}
var m = new Array(p.length + 1);
for (var i = 0; i < p.length + 1; i++) m[i] = new Array(s.length + 1);
m[0][0] = true;
i = 0;
while (i < p.length && p[i].cmd == '*') {
m[i + 1][0] = true;
i++;
}
for (var k = i; k < p.length; k++) {
m[k + 1][0] = false;
}
for (var k = 0; k < s.length; k++) {
m[0][k + 1] = false;
}
for (var x = 0; x < s.length; x++) {
for (var y = 0; y < p.length; y++) {
var xx = x + 1;
var yy = y + 1;
m[yy][xx] = (m[yy - 1][xx - 1] && s[x] == p[y].ch) || (m[yy - 1][xx] && p[y].cmd == '*') || (m[yy][xx - 1] && s[x] == p[y].ch && p[y].cmd == '*');
}
}
return m[p.length][s.length];
}
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('a', 'b*a'), true);
console.log(isMatch('a', 'a*a'), true);
console.log(isMatch('aab', 'c*a*b'), true);