Regular Expression Matching(DP)

Given an input string (s) and a pattern (p), implement regular expression matching with support for'.'and'*'.

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover theentireinput string (not partial).

Note:

  • s

    could be empty and contains only lowercase letters

    a-z

    .

  • p

    could be empty and contains only lowercase letters

    a-z

    , and characters like

    .

    or

    *

    .

Example 1:

Example 2:

Example 3:

Example 4:

Example 5:

分析

1可以顺序等得话 f[i][j] = f[i-1][j-1]

2不能的话需要*

a.把a*当做空串:f[0][j] = f[0][j-2]

b. 让s最后的字符有保障之后:if s[i-1] == p[j-2] or p[j-2] == '.' 让P匹配好s前面所有的:f[i][j] = f[i-1][j]

dp

recursive

Last updated

Was this helpful?