Skip to content

Instantly share code, notes, and snippets.

@alldroll
Last active January 21, 2020 15:14
Show Gist options
  • Save alldroll/2d1de21b4076bf29288965d0f71869ca to your computer and use it in GitHub Desktop.
Save alldroll/2d1de21b4076bf29288965d0f71869ca to your computer and use it in GitHub Desktop.
Regular Expression Matching
func isMatch(s string, p string) bool {
n, m := len(s), len(p)
dp := make([][]bool, n+1)
for i := n; i >= 0; i-- {
dp[i] = make([]bool, m+1)
}
dp[n][m] = true
for i := n; i >= 0; i-- {
for j := m - 1; j >= 0; j-- {
charMatch := i < n && (s[i] == p[j] || p[j] == dot)
if j + 1 < m && p[j+1] == asterisk {
dp[i][j] = (charMatch && dp[i+1][j]) || dp[i][j+2]
} else {
dp[i][j] = charMatch && dp[i+1][j+1]
}
}
}
return dp[0][0]
}
const (
dot = byte('.')
asterisk = byte('*')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment