【Leetcode】10. Regular Expression Matching

1 使用动态规划来做,定义一个dp table,其中dp[i][j]表示为s[0,i)和p[0,j)是否match

2 分为下面几种情况:

dp[i][j]=dp[i-1][j-1], if p[j-1]!='*' && (s[i-1]==p[j-1] or p[j-1]=='.')

dp[i][j]=dp[i][j-2], if p[j-1]=='*'and the pattern repeats for 0 times

dp[i][j]=dp[i-1][j]&&(s[i-1]==p[j-2] or p[j-2]=='.'), if p[j-1]=='*' and the pattern repeats for at least 1 times


1 corner case: 当s为空,p不为空


你可能感兴趣的:(【Leetcode】10. Regular Expression Matching)