44.go 863 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package main
  2. // ? any character; * any chatacter with any length
  3. func isMatch(s string, p string) (ans bool) {
  4. m, n := len(s), len(p)
  5. match := make([][]bool, m+1)
  6. for i := range match {
  7. match[i] = make([]bool, n+1)
  8. }
  9. match[0][0] = true
  10. for i := 1; i <= n; i++ {
  11. if p[i-1] == '*' {
  12. match[0][i] = true
  13. } else {
  14. break
  15. }
  16. }
  17. for y := 1; y <= m; y++ {
  18. for x := 1; x <= n; x++ {
  19. if s[y-1] == p[x-1] || p[x-1] == '?' {
  20. match[y][x] = match[y-1][x-1]
  21. } else if p[x-1] == '*' {
  22. // Star matches empty string or matches next character
  23. match[y][x] = match[y][x-1] || match[y-1][x]
  24. }
  25. }
  26. }
  27. return match[m][n]
  28. }
  29. // func main() {
  30. // println(isMatch("", ""))
  31. // println(isMatch("", "**"))
  32. // println(isMatch("aa", "a?"))
  33. // println(isMatch("aaa", "*"))
  34. // println(isMatch("cab", "c*b"))
  35. // println(isMatch("abc", "*c*a"))
  36. // }