Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/4bdeb0e09bd300e4364b5b9d04741a46 to your computer and use it in GitHub Desktop.
Save jianminchen/4bdeb0e09bd300e4364b5b9d04741a46 to your computer and use it in GitHub Desktop.
regular expression - using dynamic programming analysis
// Case 1: char matches = isMatch[i-1][j-1] && s[si] == p[pi]
// S "aa" "aa"
// P "aa" "ba"
// Case 2: dot matches = isMatch[i-1][j-1] && p[pi] == '.'
// S "aa" "aa"
// P "a." "b."
// Case 1 & Case 2 can be combined
// Case 3: *
// 3a - 0 matches
// s: "a" or "b" or "" or "b"
// p: "ax*" "ax*" "x*" ".*"
// T F T T
// isMatch = isMatch[i][j-2]
// 3b - 1 match
// s: "ax" or "bx" or "ab" or "ab"
// p: "ax*" "ax*" "a.*" or "c.*"
// T F T F
// isMatch = isMatch[i-1][j-2] && s[si] == p[pi - 1] || p[pi - 1] == '.'
// 3c - 1+ match
// s: "xx" or "xxx" or "bx" or "bbx"
// p: "x*" "x*" "x*" "x*"
// T T F F
// 1 will be handled in 1 match and the rest will succeed because of 0 match
// isMatch = isMatch[i-1][j] && s[si] == p[pi - 1] || p[pi - 1] == '.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment