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
| var memo map[[2]int]bool
func isMatch(s string, p string) bool { memo = make(map[[2]int]bool) return dp(s, 0, p, 0) }
func dp(s string, i int, p string, j int) bool { if j == len(p) { return i == len(s) } if i == len(s) { if (len(p) - j) % 2 == 1 { return false } for ; j + 1 < len(p); j += 2 { if p[j+1] != '*' { return false } } return true }
if v, ok := memo[[2]int{i, j}]; ok { return v }
if s[i] == p[j] || p[j] == '.' { if j + 1 < len(p) && p[j+1] == '*' { res := dp(s, i, p, j+2) || dp(s, i+1, p, j) memo[[2]int{i, j}] = res return res } else { res := dp(s, i+1, p, j+1) memo[[2]int{i, j}] = res return res } } else { if j + 1 < len(p) && p[j+1] == '*' { res := dp(s, i, p, j+2) memo[[2]int{i, j}] = res return res } else { res := false memo[[2]int{i, j}] = res return res } } }
|