leetcode44:wildcard
问题描述
给定字符串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的更多相关文章
- LeetCode44 Wildcard Matching
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- [Swift]LeetCode44. 通配符匹配 | Wildcard Matching
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- 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 ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- 【leetcode】Wildcard Matching
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 【leetcode】Wildcard Matching(hard) ★ 大神太牛了
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- 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 ...
- [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
随机推荐
- Mysql 高可用方案
1 mysql分布式数据库,如 mycat https://www.cnblogs.com/zzsdream/articles/6650690.html 2 读写分离,mysql主从复制+mysql ...
- 对Attention is all you need 的理解
https://blog.csdn.net/mijiaoxiaosan/article/details/73251443 本文参考的原始论文地址:https://arxiv.org/abs/1706. ...
- 基于IE的多标签的浏览器-世界之窗2.4
经常在Win32服务器上打开网页,但可能只是打开一个网页或是下载一点东西而已,虽然默认的IE浏览器基本上能满足需求 但还是存在不少的不便,比如如果要打开多个网页的话则需要再打开一个IE,还有就是习惯了 ...
- Internet传输协议-TCP
http://phei.eefocus.com/book/08-07/473781276058574.html http://www.eefocus.com/communication/210643 ...
- Summarizing NUMA Scheduling两篇文章,解释得不错
http://vxpertise.net/2012/06/summarizing-numa-scheduling/ Sitting on my sofa this morning watching S ...
- Shiro整合springboot,freemaker,redis(含权限系统完整源码)
区块链技术联盟 2018-02-08 17:06:40 目录 一.导语 二.shiro功能介绍 三.shiro详解 四.shiro实战案例分享 五.系统配置 六.其他 一.导语 今天推荐给大家一个非常 ...
- Data De-duplication
偶尔看到data deduplication的博客,还挺有意思,记录之 http://blog.csdn.net/liuben/article/details/5829083?reload http: ...
- Array相关的属性和方法
这里只是做了相关的列举,具体的使用方法,请参考网址. Array 对象属性 constructor 返回对创建此对象的数组函数的引用. var test=new Array(); if (test.c ...
- iframe 与 frame 区别
1.iframe iframe主要来内联一个外联的页面,如: <!DOCTYPE html> <html lang="zh"> <head> & ...
- ES6 Generator async
Generator 函数是 ES6 提供的一种异步编程解决方案 async 函数使得异步操作变得更加方便,是 Generator 函数的语法糖. js单线程的原因是:避免DOM渲染冲突! 更新:201 ...