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 --------------------------------------------------------------- ...
随机推荐
- Android -- 在xml文件中定义drawable数组
Xml <string-array name="images"> <item>@drawable/image1</item> <item& ...
- 使用word2vec训练中文词向量
https://www.jianshu.com/p/87798bccee48 一.文本处理流程 通常我们文本处理流程如下: 1 对文本数据进行预处理:数据预处理,包括简繁体转换,去除xml符号,将单词 ...
- [React] Asynchronously Load webpack Bundles through Code-splitting and React Suspense
One approach to building high performance applications with webpack is to take advantage of code-spl ...
- VC++导出具有命名空间的函数
问题现象 原因分析 解决的方法 1 问题现象 导出具有命名空间的函数和类.源码例如以下: 头文件MiniMFC.h namespace MiniMFC { __declspec(dllexport) ...
- SDE操作的许可问题
ArcGIS二次开发和ArcGIS桌面应用中,许可是一个老生常谈的问题.以前也小结过一些经验.参考: http://www.cnblogs.com/liweis/p/4185311.html 问题描述 ...
- iOS 两个应用之间的切换
A 跳到B NSURL *urlT = [NSURL URLWithString:@"TestB://XXXXXXX"]; //注意“://”后面可以任意传参数.这些参数传过去后当 ...
- ios 开发 ping
在win 或 mac系统的终端下都有方便的ping命令来检测网络的连通性! 在iOS上可以使用苹果封装好的工具来开发ping连接测试 SimplePing 下载 1. 主要接口 //初始化一个地址 - ...
- typescript - 前言介绍
众所周知,JavaScript是弱语言(子承父业都表达不了),因此比较大的企业考虑到可维护性以及安全性来说,都不用它开发,因此Typescript诞生了,它并不是为了替换JavaScript而诞生的, ...
- VMware的存储野心(上):软件定义、分布式DAS支持
ChinaByte比特网 http://storage.chinabyte.com/291/12477791_2.shtml 11月29日(文/黄亮)- SDN(软件定义的网络,Software De ...
- 【转】跟我一起学Spring 3(4)–深入理解IoC(控制反转)和DI(依赖注入)
在继续下面的章节之前,我们要先说说大名鼎鼎的IoC和DI. 我们经常会听说IoC,也就是Inversion of Controller,控制反转.事实上,IoC并不是一个新鲜的概念,最早可能是在198 ...