44. Wildcard Matching

问题描述

给定字符串s和模式p,判断字符串s是否完全符合模式p

其中字符串s只包含小写字母,模式串p包含小写字母、*?,其中星号表示任意长度的任意字符串,问号表示任意一个字符(不能是空)。

解决思路

这么小的问题,不至于使用正则表达式。

即便使用正则表达式可以解决,那也肯定不如特殊问题特殊处理速度快、效率高。

这个问题中‘?’还好解决,关键是星号。一种很直观的思路就是:a=p.split("*"),将字符串p使用星号进行分隔,得到一个字符串数组a,只要s中顺次包含a中的全部字符串,那就必然匹配成功。

这么实现需要注意的点是:当s="mabcd",p="ab*cd"时,s确实包含ab和cd,但是ab必须得作为开头存在,否则程序就出错了。最好的解决方法就是添加哨兵单元,在字符串s和p的首尾各加上一个特殊字符,如$,^等。

class Solution:
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
a = []
now = ''
p = '^' + p + "$"
s = '^' + s + '$'
for i in p:
if i == '*':
if len(now):
a.append(now)
now = ''
else:
now = now + i
if len(now):
a.append(now)
i = 0
j = 0
while i < len(s) and j < len(a):
if self.ok(s, i, a[j]):
i += len(a[j])
j += 1
else:
i += 1
return j == len(a) and i == len(s) def ok(self, s, i, ss):
if i + len(ss) > len(s): return False
for j in range(len(ss)):
if ss[j] != s[i + j] and not ss[j] == '?':
return False
return True if __name__ == '__main__':
s = Solution()
for i in (("a", "*"),
("acb", '*a?b*'),
(
"babbbbaabababaabbababaababaabbaabababbaaababbababaaaaaabbabaaaabababbabbababbbaaaababbbabbbbbbbbbbaabbb",
"b**bb**a**bba*b**a*bbb**aba***babbb*aa****aabb*bbb***a")):
print(i[0], i[1], s.isMatch(i[0], i[1]))

leetcode44:wildcard的更多相关文章

  1. LeetCode44 Wildcard Matching

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

  2. [Swift]LeetCode44. 通配符匹配 | Wildcard Matching

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

  3. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.

    spring 配置文件报错报错信息:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be ...

  4. [LeetCode] Wildcard Matching 外卡匹配

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  5. 【leetcode】Wildcard Matching

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  6. 【leetcode】Wildcard Matching(hard) ★ 大神太牛了

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  7. A Simple C++ Template Class that Matches a String to a Wildcard Pattern

    A recently implemented enhanced wildcard string matcher, features of which including, Supporting wil ...

  8. [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  9. LeetCode - 44. Wildcard Matching

    44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...

随机推荐

  1. distinct 多列详解

    1.distinct单列 select distinct(a) from tableA; 2.distinct多列 select distinct a,b,c from tableA; 注意此时是将a ...

  2. angularJs中$controller的使用

    $controller的使用 参考:https://stackoverflow.com/questions/27866620/can-someone-provide-a-use-case-for-th ...

  3. Mongo = get size of single document

      Object.bsonsize(db.test.findOne({type:"auto"}))

  4. [Algorithm] Reverse a linked list

    It helps to understands how recursive calls works. function Node(val) { return { val, next: null }; ...

  5. [Canvas]计时表/秒表

    欲观看效果请点击下载,然后用浏览器打开index.html查看. 本作 Github地址:https://github.com/horn19782016/StopWatch 图例: 代码: <! ...

  6. maven 上传包

    本地 mvn deploy 第三方jar包 mvn deploy:deploy-file -DgroupId=com.zendaimoney.uc -DartifactId=uc-client -Dv ...

  7. 解决input框中加入disabled="disabled"之后,改变字体的颜色(默认的是灰色)

    在input框中加入disabled="disabled"之后,字体默认的就变成灰色了 解决方案 input[disabled]{color:#fff;opacity:1} dis ...

  8. ZH奶酪:PHP 使用DOMDocument抓取网页

    原文链接:http://blog.csdn.net/xyzhaopeng/article/details/6626340 从一个HTML页面的一个表格中提取数据并且将这个数据整理出来加入到MySQL数 ...

  9. C++ 第六课:C/C++关键字及其用法

    asm 插入一个汇编指令. auto 声明一个本地变量. bool 声明一个布尔型变量. break 结束一个循环. case 一个switch语句的一部分. catch 处理 thrown 产生的异 ...

  10. SRA秘钥生成与解密

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java ...