[LeetCode][Python]Regular Expression Matching
# -*- coding: utf8 -*-
'''
https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.
'*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be:
bool isMatch(const char *s, const char *p) Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true ===Comments by Dabay===
自我感觉很解得很垃圾啊。如果不是把p做了压缩还过不了online judge。其实就是一个DFA。 当s和p都是空的时候,匹配成功。
如果s为空,p不为空,检查p的偶数为是不是都是*。 如果s和p都不为空,头一个字母
如果不匹配,
不带*,False
带*,递归p[2:]
如果匹配,
不带*,递归s[1:],p[1:]
带*,考虑匹配0到最远端的情况,分别递归s[i:],p[2:]
'''
class Solution:
# @return a boolean
def isMatch(self, s, p):
def compress(p):
i = 0
while i < len(p)-3:
if p[i+1] != '*':
i = i + 1
continue
if p[i+3] == '*':
if p[i] == "." or p[i+2] == ".":
p = p[:i] + ".*" + p[i+4:]
continue
elif p[i] == p[i+2]:
p = p[:i] + p[i+2:]
continue
i = i + 2
return p def isMatch2(s, p):
if len(s) == 0:
if len(p) == 0:
return True
if len(p) % 2 == 0:
i = 1
while i < len(p):
if p[i] != "*":
return False
i = i + 2
else:
return True
else:
return False
if len(s) > 0 and len(p) == 0:
return False match_char = p[0]
multi = False
if len(p) > 1:
if p[1] == "*":
multi = True if match_char != s[0] and match_char != ".":
if multi:
return isMatch2(s, p[2:])
else:
return False if multi is False:
return isMatch2(s[1:], p[1:])
else:
result = False
i = 0
result = isMatch2(s, p[2:])
while i < len(s):
if result == True:
return result
if (s[i] == match_char or match_char == "."):
result = isMatch2(s[i+1:], p[2:])
else:
break
i = i + 1
return result return isMatch2(s, compress(p)) def main():
s = Solution()
print s.isMatch("aa", "a*") if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[LeetCode][Python]Regular Expression Matching的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
- 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- bootstrap简章
系统整理一遍bootstrap 的东西 1/ 设置页面为H5文档类型 <!DOCTYPE html> <html lang="zh-CN"> ... &l ...
- 解决jquery zclip 插件点击无效的问题
使用jquery zclip 用于页面复制文本内容. 首先引入js <script type="text/javascript" src="../js/jquery ...
- Windows 打开防火墙上的指定端口
第一步: 控制面板-->打开防火墙 第二步: 高级设置 第三步: 入站规则--->新建规则 第四步: 第五步: 第六步: 第七步: 第八步:
- 限制TextBox输入,只能输入整数
public class TextBoxInt : TextBox { public TextBoxInt() { KeyDown += TextBoxInt_KeyDown; TextChanged ...
- 在chart上加入一条指示线
原文 http://hi.baidu.com/fuwei_bj/item/7e576410e970683db831801a <mx:AreaChart width="521" ...
- ORACLE RAC中一个实例不能随crs自动启动的解决
现象:在两个节点上做CRS的重启,这个实例都不能随CRS的启动而启动.CRS启动后做crs_start -all可以把没启动的资源起来,而且无报错. 分析:去crsd.log中找原因,发现CRS根本就 ...
- 50% 的财富 500 强企业使用 Windows Azure
在上周的北美TechEd大会上,我有幸见到了来自世界各地的客户.合作伙伴和分析师,其数量之多,让人震惊.没有什么比亲耳聆听使用 Windows Azure 来开创新天地的客户亲口讲述他们的故事更令人振 ...
- jQuery插件
1. Lightbox 2: Lightbox 2 是一款很酷的图片显示插件,可以在一个平滑展开的新窗口中展现出图片,而同时页面的其余部分都变成黑色背景.使用这款插件,是为了当你的的图片过大必须 ...
- spring security执行流程图
今天看到非常多人转载了这篇文章,这里备注一下.原文来自CSDN我的博客. 近期在研究spring security的配置,研究了一个星期了,在官网看了下.仅仅配置出来了简单的登录,但不知如何从数据库读 ...
- 微软将Bing变开放平台 同谷歌争夺开发者
微软在编译者大会上宣布将Bing作为平台开放,此举显然旨在改变谷歌(微博)一家独大的局面. 报道称,微软知道如何创建平台.因此当它发布新平台时,都值得业界仔细关注.就在上周之前,微软Bing给大家的印 ...