leetcode-10-Regular Expression Matching

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zem_nezer/article/details/86688694

Error:
do not know how to do:
in dp, it need to figure out something:
1) state
2) init
3) func
4) return
in here,
1)define dp[i][j] as if s and p is match or not in i len and j len(not index)
2) init all as false, except dp[0][0], a.k.a. two null string is true. and also null* is true
3) so that we have transform equations:

dp[i][j] = dp[i - 1][j - 1] && s[i - 1] == p[j - 1]
dp[i][j] = dp[i - 1][j - 1] if p[j - 1] == '.'
dp[i][j] = dp[i - 1][j] || dp[i][j - 2] if p[j - 1] == '* ' && (p[j - 2] == '.' || p[j - 2] == s[i - 1])
dp[i][j] = dp[i][j - 2] if p[j - 1] == '*'
	4) return dp[s_len][p_len]

the difficult part is handle ‘*’ it have two cases: we have multiple p[j - 2](i.e. preceding char) or use zero p[j -2](i.e. zero preceding). If multiple, then it should equal to dp[i - 1][j], i.e. its previous state; if it has zero, then it equal to dp[i][j - 2]

猜你喜欢

转载自blog.csdn.net/zem_nezer/article/details/86688694