dp
with dimensions (len(s) + 1) x (len(p) + 1)
.dp[0][0]
to True
to represent that an empty string matches an empty pattern.'*'
by setting dp[0][j]
to True
if dp[0][j-2]
is True
.s
and p
using indices i
and j
.p[j-1]
is '.'
or s[i-1] == p[j-1]
, set dp[i][j]
to the value of dp[i-1][j-1]
.p[j-1]
is '*'
, check two conditions:
a. If the pattern before '*'
matches zero occurrences, set dp[i][j]
to dp[i][j-2]
.
b. If the pattern before '*'
matches one or more occurrences, set dp[i][j]
to dp[i-1][j]
if s[i-1]
matches p[j-2]
.dp[len(s)][len(p)]
.