[Leetcode][Python]44:Wildcard Matching
- # -*- 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的更多相关文章
- LeetCode(44) Wildcard Matching
题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single characte ...
- [LeetCode]题解(python):044-Wildcard Matching
题目来源: https://leetcode.com/problems/wildcard-matching/ 题意分析: 定义两个新字符规则,'?'代表任意一个字符,’*‘代表任意长度的任意字符.输入 ...
- LeetCode第[44]题(Java):Wildcard Matching
题目:通配符匹配 难度:hard 题目内容: Given an input string (s) and a pattern (p), implement wildcard pattern match ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- 每日算法之三十五:Wildcard Matching
模式匹配的实现,'?'代表单一字符,'*'代表随意多的字符.写代码实现两个字符串是否匹配. Implement wildcard pattern matching with support for ' ...
- leetcode 第43题 Wildcard Matching
题目:(这题好难.题目意思类似于第十题,只是这里的*就是可以匹配任意长度串,也就是第十题的‘.*’)'?' Matches any single character. '*' Matches any ...
- [LeetCode][Python]15:3Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 15: 3Sumhttps://oj.leetcode.com/problem ...
- LeetCode 044 Wildcard Matching
题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...
- [LeetCode] 44. Wildcard Matching 外卡匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
随机推荐
- MFC界面开发(QQ透明皮肤:多层算法,一键适配各种背景 )
http://blog.csdn.net/kent19900125/article/category/1368203/3 QQ透明皮肤:多层算法,一键适配各种背景 . http://blog.csdn ...
- C语言中typedef
一.基本概念剖析 ])(int, char*); //#1 ]) (void (*)()); //#2 ]; //#3 .C语言中函数声明和数组声明.函数声明一般是这样: int fun(int, d ...
- 深入浅出Node.js (2) - 模块机制
2.1 CommonJS规范 2.1.1 CommonJS的出发点 2.1.2 CommonJS的模块规范 2.2 Node的模块实现 2.2.1 优先从缓存加载 2.2.2 路径分析和文件定位 2. ...
- 用于下载AD官网登录账号:User name: fuxin918@fuxin918.com Passeword: s6c0W1w8
用于下载AD官网登录账号:User name: fuxin918@fuxin918.com Passeword: s6c0W1w8
- python手记(44)
#!/usr/bin/env python # -*- coding: utf-8 -*- #http://blog.csdn.net/myhaspl #code:myhaspl@qq.com imp ...
- PHP 表单验证 - 验证 E-mail 和 URL
----------------------------------------------------------------------------- 本节展示如何验证名字.电邮和 URL. -- ...
- poj 3678 Katu Puzzle(2-sat)
Description Katu Puzzle ≤ c ≤ ). One Katu ≤ Xi ≤ ) such that for each edge e(a, b) labeled by op and ...
- js埋点(转载)
页面埋点的作用,其实就是用于流量分析.而流量的意思,包含了很多:页面浏览数(PV).独立访问者数量(UV).IP.页面停留时间.页面操作时间.页面访问次数.按钮点击次数.文件下载次数等.而流量分析又有 ...
- 六度分离(floyd算法+dijskra+SPFA)
六度分离 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- mvc 日历控件
第二个是日历控件,在网上查了一个普通的日历控件,也生成了下拉的日历样子,但是一些脚本比如选择年月,需要一些时间,最后只好套用了My97 DatePicker,这样以来其实简单多了. 第一步:下载 My ...