# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matching
https://oj.leetcode.com/problems/wildcard-matching/ '?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
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", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false ===Comments by Dabay===
使用动态规划的做法.
先想象在s和p的前面都加一个空格,以s为外循环,p为内循环。
先初始化第一组数据,第一个“空”匹配“空”,所以第一个为True。然后看如果后面如果是*就继续设置为True。 进入循环之后,判断根据p中的字符
- 如果是?*以外的字符,就判断这个字符和s中对应的字符是否一致,而且s中到上一个字符和p中到上一次字符匹配,设True
- 如果是?,如果s中到上一个字符和p中到上一个字符匹配,设True
- 如果是*,满足下面的情况,就设True
- (匹配0次)s中到这个字符和p到上两个字符匹配
- (匹配1次)s中到这个字符和p到上一个字符匹配
- (匹配n次)s中到上一个字符和p到这个字符匹配
''' class Solution:
# @param s, an input string
# @param p, a pattern string
# @return a boolean
def isMatch(self, s, p):
def quick_test(s, p):
num_of_star = 0
for x in p:
if x == "*":
num_of_star = num_of_star + 1
return len(s) >= len(p)-num_of_star if quick_test(s, p) is False:
return False
default_row = [False] + [False for _ in p]
current_row = list(default_row)
current_row[0] = True
for j in xrange(len(p)):
if p[j] == "*":
current_row[j+1] = True
else:
break
previous_row = current_row
for i in xrange(len(s)):
current_row = list(default_row)
for j in xrange(len(p)):
if p[j] == "?":
if previous_row[j]:
current_row[j+1] = True
elif p[j] == "*":
if current_row[j] or previous_row[j] or previous_row[j+1]:
current_row[j+1] = True
else:
if p[j] == s[i] and previous_row[j]:
current_row[j+1] = True
previous_row = current_row
return previous_row[-1] def main():
sol = Solution()
s = "ac"
p = "ab*"
print sol.isMatch(s, p) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]44:Wildcard Matching的更多相关文章

  1. LeetCode(44) Wildcard Matching

    题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single characte ...

  2. [LeetCode]题解(python):044-Wildcard Matching

    题目来源: https://leetcode.com/problems/wildcard-matching/ 题意分析: 定义两个新字符规则,'?'代表任意一个字符,’*‘代表任意长度的任意字符.输入 ...

  3. LeetCode第[44]题(Java):Wildcard Matching

    题目:通配符匹配 难度:hard 题目内容: Given an input string (s) and a pattern (p), implement wildcard pattern match ...

  4. [Leetcode][Python][DP]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

  5. 每日算法之三十五:Wildcard Matching

    模式匹配的实现,'?'代表单一字符,'*'代表随意多的字符.写代码实现两个字符串是否匹配. Implement wildcard pattern matching with support for ' ...

  6. leetcode 第43题 Wildcard Matching

    题目:(这题好难.题目意思类似于第十题,只是这里的*就是可以匹配任意长度串,也就是第十题的‘.*’)'?' Matches any single character. '*' Matches any ...

  7. [LeetCode][Python]15:3Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 15: 3Sumhttps://oj.leetcode.com/problem ...

  8. LeetCode 044 Wildcard Matching

    题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...

  9. [LeetCode] 44. Wildcard Matching 外卡匹配

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

随机推荐

  1. Apache、php、mysql单独安装配置

    php, 安装版的,http://www.php.net/manual/zh/install.php.也有不安装版直接配置的. 在Windows 7下如何进行PHP配置环境. PHP环境在Window ...

  2. cf478D Red-Green Towers

    D. Red-Green Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. CSS发光边框文本框效果

    7,166 次阅读 ‹ NSH Blog 网页设计 CSS发光边框文本框效果 或许你看过Safari浏览器下,任何输入框都会有一个发光的蓝色边框,这不单纯只是蓝色边框而已,其实包含了许多CSS3技巧知 ...

  4. Master Theorem

    Master theorem provides a solution in asymptotic terms to solve time complexity problem of most divi ...

  5. GridBagLayout练习

    摘自http://blog.csdn.net/qq_18989901/article/details/52403737  GridBagLayout的用法 GridBagLayout是面板设计中最复杂 ...

  6. jquery第二期:三个例子带你走进jquery

    jquery是完全支持css的,我们举个例子来看看使用jquery的方便之处,这功劳是属于选择器的: 例1: <!DOCTYPE html PUBLIC "-//W3C//DTD HT ...

  7. 449A - Jzzhu and Chocolate 贪心

    一道贪心题,尽量横着切或竖着切,实在不行在交叉切 #include<iostream> #include<stdio.h> using namespace std; int m ...

  8. 从一段代码看fork()函数及其引发的竞争

    首先来看一段从<UNIX环境高级编程>中摘录的一段很有意思的代码.借此我们再来谈谈fork()函数的一些问题. #include "apue.h" static voi ...

  9. 天坑 之 Eclipse J2EE Preview 运行正确项目一直显示http 404

    昨天下载了几个新Demo学习,结果不知道改了哪里,导致运行原先自己写的项目(JSP+Servlet+JDBC)(这理论上不会出什么大的问题吧?这么底层),结果莫名其妙的出现Http 404. 搞的我一 ...

  10. 也谈谈关于WEB的感想

    距离上次在博客园发表博文已经是数年以前了,想想自己也确实有够懒惰的,实为不该. 引起我想发这篇博文的原因是 @Charlie.Zheng 所发表的 <Web系统开发构架再思考-前后端的完全分离& ...