leetcode10. Regular Expression Matching
There are three special combination
- ‘.’
- ‘.*’
- ‘b*’, (b means a specific letter)
Pseudo-Code
//Build up a M.LEN*N.LEN matrix dp, where M is the matching string, and N is the compared string
For i from 0 to M:
For j from 0 to N:
// This is the most basic situation
IF M[i] == N[i] || M[i] == '.':
dp[i][j] = dp[i-1][j-1]
END IF
// The Dp equation
// 1. the number is zero
IF M[i] == '*':
dp[i][j] = dp[i-2][j] || dp[i][j]
// 2. the number is one or more
IF M[i-1] == N[j] || M[i-1] == '.':
dp[i][j] = dp[i][j-1]
END IF
END FOR
END FOR