[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 ...
随机推荐
- 【原】从一个bug浅谈YUI3组件的资源加载
篇前声明:为了不涉及业务细节,篇内信息统一以某游戏,某功能代替 前不久,某游戏准备内测客户端,开发人员测试过程中发现某功能突然不灵了,之前的测试一切ok,没有发现任何异常,第一反应是,游戏内浏览器都是 ...
- Javascript 多物体运动——逐行分析代码,让你轻松了解运动的原理
我们先来看下之前的运动的代码,是否支持多物体运动,会出现怎么样的问题. <style type="text/css"> div { width: 100px; heig ...
- php 截取字符串
/** * 方法库-截取字符串-[该函数作者未知] * @param string $string 字符串 * @param int $length 字符长度 * @param string $dot ...
- c语言字符串翻转系列
2013-10-25 最近碰到一道笔试题,是关于字符串翻转的.题目是:将一段英文翻转,但保留单词拼写,如给定字符串str="I am a student",返回为"stu ...
- 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)
确定比赛名次 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- poj 2686 Traveling by Stagecoach ---状态压缩DP
题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马. 点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) ...
- UIViewController、UINavigationController与UITabBarController的整合使用
UINavigationController与UITabBarController是iOS开发中最常用的两种视图控制器,它们都属于UIViewController的子类,继承关系如下: @interf ...
- mysql distinct&group by 应用
在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的 ...
- js charts去掉logo
打开js charts 3的源代码搜索关键字"fs.bg",然后会找到 fs.bg.2v(fX),将这句代码删掉就OK了,可能有的版本会是fs.bg.2u(fX) 欢迎加入群:25 ...
- @Html.ValidationSummary()的使用
@Html.ValidationSummary()用于返回表单在后台验证的结果. 如, 当后台if (ModelState.IsValid)失败后,错误信息就会显示到 @Html.Validation ...