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 --------------------------------------------------------------- ...
随机推荐
- 最小二乘法多项式曲线拟合原理与实现 zz
概念 最小二乘法多项式曲线拟合,根据给定的m个点,并不要求这条曲线精确地经过这些点,而是曲线y=f(x)的近似曲线y= φ(x). 原理 [原理部分由个人根据互联网上的资料进行总结,希望对大家能有用] ...
- 【math】梯度下降法(梯度下降法,牛顿法,高斯牛顿法,Levenberg-Marquardt算法)
原文:http://blog.csdn.net/dsbatigol/article/details/12448627 何为梯度? 一般解释: f(x)在x0的梯度:就是f(x)变化最快的方向 举个例子 ...
- 逻辑回归应用之Kaggle泰坦尼克之灾
机器学习系列(3)_逻辑回归应用之Kaggle泰坦尼克之灾 标签: 机器学习应用 2015-11-12 13:52 3688人阅读 评论(15) 收藏 举报 本文章已收录于: 机器学习知识库 分类 ...
- android中使用toolbar
系统默认使用的是ActionBar,就是界面中的标题栏,但是由于ActionBar设计的原因,被限定只能位于活动的顶部,从而不能实现Material Design效果,所以官方建议使用Toolbar替 ...
- C++ 第九课 标准c数学函数
abs() 求绝对值 acos() 求反余弦 asin() 求反正弦 atan() 求反正切 atan2() 求反正切,按符号判定象限 ceil() 求不小于某值的最小整数 (求上界) cos() 求 ...
- 详解JAVA输出Hello World
想必大家对这一段JAVA代码一定不会陌生: public class Test { public static void main(String[] args) { System.out.printl ...
- python unittest case运行失败重试
因为使用unittest进行管理case的运行.有时case因为偶然因素,会随机的失败.通过重试机制能够补充保持case的稳定性.查阅资料后发现,python的unittest自身无失败重试机制,可以 ...
- Vim 中如何去掉 ^M 字符
基于 DOS/Windows 的文本文件在每一行末尾有一个 CR(回车)和 LF(换行),而 UNIX 文本只有一个换行,即win每行结尾为\r\n,而linux只有一个\n如果win下的文档上传到l ...
- Aerospike系列:4:简单的增删改查aql
[root@localhost bin]# aql --help Usage: aql OPTIONS OPTIONS -h <host> The hostname to the serv ...
- 行内元素有哪些?块级元素有哪些? 空(void)元素有那些?
行内元素:a.b.span.img.input.strong.select.label.em.button.textarea块级元素:div.ul.li.dl.dt.dd.p.h1-h6.blockq ...