LeetCode Regular Expression Matching
来源:互联网 发布:杜兰特新秀赛季数据 编辑:程序博客网 时间:2024/06/09 16:50
LeetCode解题之Regular Expression Matching
原题
简易版正则表达式匹配,只有两种通配符,”.”表示任意一个字符,”c*”表示字符c可以有零个或多个。
注意点:
- 存在一些不合理的字符组合,如”**”
- “.*”可以表示任意字符串
- 需要匹配整个目标串,而不是部分
例子:
输入: s=”aab”, p=”c*a*b”
输出: True
解题思路
开始用递归实现了一遍,结果超时,改用动态规划。dp[i][j]表示s[i:]和p[j:]的匹配情况,围绕”*”进行分类讨论,分类的情况比较多,具体请参看代码注释。
AC源码
class Solution(object): def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ m = len(s) n = len(p) # Init dp dp = [[False for i in range(n + 1)] for i in range(m + 1)] # When string and pattern are all None dp[m][n] = True # When the string is None, pattern like "a*" can still match it for i in range(n - 1, -1, -1): if p[i] == "*": dp[m][i] = dp[m][i + 1] elif i + 1 < n and p[i + 1] == "*": dp[m][i] = dp[m][i + 1] else: dp[m][i] = False for i in range(m - 1, -1, -1): for j in range(n - 1, -1, -1): # When the current character is "*" if p[j] == "*": if j - 1 >= 0 and p[j - 1] != "*": dp[i][j] = dp[i][j + 1] # If the pattern is starting with "*" or has "**" in it else: return False # When the the second character of pattern is "*" elif j + 1 < n and p[j + 1] == "*": # When the current character matches, there are three possible situation # 1. ".*" matches nothing # 2. "c*" matches more than one character # 3. "c*" just matches one character if s[i] == p[j] or p[j] == ".": dp[i][j] = dp[i][j + 2] or dp[i + 1][j] or dp[i + 1][j + 2] # Ignore the first two characters("c*") in pattern since they cannot match # the current character in string else: dp[i][j] = dp[i][j + 2] else: # When the current character is matched if s[i] == p[j] or p[j] == ".": dp[i][j] = dp[i + 1][j + 1] else: dp[i][j] = False return dp[0][0]if __name__ == "__main__": assert Solution().isMatch("aa", "a") == False assert Solution().isMatch("aa", "aa") == True assert Solution().isMatch("aaa", "aa") == False assert Solution().isMatch("aa", "a*") == True assert Solution().isMatch("aa", ".*") == True assert Solution().isMatch("ab", ".*") == True assert Solution().isMatch("aab", "c*a*b") == True
欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。
0 0
- LeetCode: Regular Expression Matching
- LeetCode: Regular Expression Matching
- leetcode Regular Expression Matching
- [Leetcode] Regular Expression Matching
- [LeetCode]Regular Expression Matching
- LeetCode-Regular Expression Matching
- Regular Expression Matching LeetCode
- LeetCode | Regular Expression Matching
- LeetCode: Regular Expression Matching
- Leetcode: Regular Expression Matching
- [LeetCode] Regular Expression matching
- [LeetCode] Regular Expression Matching
- LeetCode Regular Expression Matching
- leetcode Regular Expression Matching
- leetcode: Regular Expression Matching
- LeetCode Regular Expression Matching
- Leetcode: Regular Expression Matching
- leetcode:Regular Expression Matching
- NSURLConnection (iOS9已经过期)
- 关键字Static/Const的作用
- UIScrollView的滚动,停止(UIScrollViewDelegate)
- ia32中程序调用返回时call.ret.leave的作用和栈变换的说明
- 用Open vSwitch实现VLAN
- LeetCode Regular Expression Matching
- ListView图片优化(二)---弱引用关联---或者使用Volley下的NetworkImageView
- PHP iconv()字符编码转换的问题
- 欢迎使用CSDN-markdown编辑器
- Spring4+Quartz2计划任务集群
- grant create synonym to SCOTT
- 在阿里云ubuntu 14.04 上部署Gogs
- 汉洛塔递归实现的思考(C语言)
- hdoj1379DNA Sorting