Skip to content

Commit dd4c423

Browse files
author
Yeqi Tao
committed
Add Soulution2.go for 0010.Regular Expression Matching
1 parent 147f595 commit dd4c423

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
func isMatch(s string, p string) bool {
2+
lenS := len(s)
3+
lenP := len(p)
4+
// init dp note
5+
note := make([][]int8, lenS+1)
6+
for i:=0; i<=lenS; i++ {
7+
note[i] = make([]int8, lenP+1)
8+
for j:=0; j<=lenP; j++ {
9+
note[i][j] = -1
10+
}
11+
}
12+
13+
return dp(0, 0, s, p, note)
14+
}
15+
16+
func dp(i,j int, s,p string, note [][]int8) bool {
17+
if note[i][j] != -1 {
18+
return note[i][j] == 1
19+
}
20+
var ans bool
21+
if j == len(p) {
22+
ans = i == len(s)
23+
} else {
24+
fm := i < len(s) && (s[i] == p[j] || p[j] == '.')
25+
if j + 1 < len(p) && p[j+1] == '*' {
26+
ans = dp(i, j+2, s, p, note) || (fm && dp(i+1, j, s, p, note))
27+
} else {
28+
ans = fm && dp(i+1, j+1, s, p, note)
29+
}
30+
}
31+
32+
if ans {
33+
note[i][j] = 1
34+
} else {
35+
note[i][j] = 0
36+
}
37+
return ans
38+
}

0 commit comments

Comments
 (0)