leetcode-10-Regular Expression Matching

本文深入探讨了正则表达式匹配的动态规划算法,详细解释了状态定义、初始化、状态转移方程以及如何处理特殊字符'*'。通过具体实例,帮助读者理解算法的核心思想。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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] == '*'
  1. 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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值