6. Wildcard Matching

题目

Implement wildcard pattern matching with support for '?' and '*'.

'?' 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") ? falseisMatch("aa","aa") ? trueisMatch("aaa","aa") ? falseisMatch("aa", "*") ? trueisMatch("aa", "a*") ? trueisMatch("ab", "?*") ? trueisMatch("aab", "c*a*b") ? false

解答

DFS

这里的难点在于如何处理*,因为这个星号可以代表0到多个字符,而且有可能会遇到递归一开始匹配正确后面不正确,但实际上应该从后面开始匹配。

class Solution(object):
# p为匹配模式,s为字符串
def recursive(self, s, p, si, pi, cur):
first = True
n_cur = cur
while si < len(s) and pi < len(p) and (s[si] == p[pi] or p[pi] == '?'):
si += 1
pi += 1
if pi == len(p):
return si == len(s)
if p[pi] == '*':
while pi < len(p) and p[pi] == '*':
pi += 1
if pi >= len(p):
return True
for i in range(si, len(s)):
# 表明开始重合,从这里再度开始递归
if p[pi] != s[i] and p[pi] != '?':
continue
if first:
cur += 1
first = False
# 可能存在多次重合但是还不算真正匹配的情况
if self.recursive(s, p, i, pi, cur + 1):
return True
if cur > n_cur + 1: # 正常来说n_cur = cur + 1
return False
return False
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
return self.recursive(s, p, 0, 0, 0)

这种做法超时。

DP

我们定义一个二维数组dp,横坐标为待匹配字符串,纵坐标为模式字符串,dp[i][j]则代表到模式字符串从0到 i 对应待匹配字符串的的0到 j 是否是匹配的。举个例子:

pattern = "a*bc"
str = "abbc"

我们可以根据前面提到的画出整个二维数组

\ \ a b b c
\ T F F F F
a F T F F F
* F T T T T
b F F T T F
c F F F F T

我们可以发现一个规律,每当遇到两个字符不相等的时候,那么数组的值则肯定是False,相反相等的时候肯定是True,这里需要注意的是*,这里则需要考虑到它当前可能匹配0个字符串或者匹配多个字符,比如上面中的a*ab的情况,此时我们需要发现a*及a或者a和ab其中有任何一个成功匹配的,它的结果也肯定为T。

这个状态转义方程要怎么推算出来呢?

  1. 如果p.charAt(i)=='*','*'可以选择匹配0个字符,此时flag[i][j]=flag[i-1][j];可以选择匹配1个字符,此时flag[i][j]=flag[i-1][j-1];……所以可以得到下面的公式:

  2. 因为flag[i][j]=flag[i-1][j]||flag[i-1][j-1]||……||flag[i-1][0],我们可以代入上面的公式得到:

于是我们可以很简单的写出程序了(下面的程序的i,j和状态转义方程是相反的,但是原理是相同的)

class Solution(object):
# p为匹配模式,s为字符串
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
if len(s) != len(p) - p.count('*'):
return False
newp = ""
i = 0
while i < len(p):
newp += p[i]
if p[i] == '*':
while i + 1 < len(p) and p[i + 1] == '*':
i += 1
i += 1
sl, pl = len(s), len(newp)
dp = [[False for x in range(pl + 1)] for y in range(sl + 1)]
dp[0][0] = True
if pl > 0 and p[0] == '*':
dp[0][1] = True
for x in range(1, sl + 1):
for y in range(1, pl + 1):
if newp[y - 1] != '*':
dp[x][y] = dp[x - 1][y - 1] and (s[x - 1] == newp[y - 1] or newp[y - 1] == '?')
else:
dp[x][y] = dp[x - 1][y] or dp[x][y - 1]
return dp[sl][pl]

同样的原理,我们还可以把它缩减成一维数组,你可以把它想象成在二维数组中计算每一行的数据,如果遇到*则更新当前行的数据;为什么可以这么做呢?我们可以根据前面提到的公式发现,其中当前的数据依赖于j的变化,也就是待匹配字符串的值,我们还需要在外面写个模式串的循环,其实和二维数组的做法的时间复杂度是一样的,但是缩减了空间,但是并不是所有的都可以这么做,这个取决于你的依赖项是什么。总而言之,其原理还是一样的,只是想办法让它们的数据能够共存到一维数组中。

class Solution:
# @return a boolean
def isMatch(self, s, p):
length = len(s)
if len(p) - p.count('*') > length:
return False
dp = [True] + [False]*length
for i in p:
if i != '*':
# 因为依赖项是前面的值,所以不能从前面往后面扫,得从后往前计算
for n in reversed(range(length)):
dp[n+1] = dp[n] and (i == s[n] or i == '?')
else:
# 更新当前行的数据
for n in range(1, length+1):
dp[n] = dp[n-1] or dp[n]
dp[0] = dp[0] and i == '*'
return dp[-1]

贪心算法

下标 描述
si 待匹配字符串的移动下标
pi 模式串的移动下标
lastmatch 上一次匹配的待匹配字符串的下标
laststar 上一次匹配的模式串的下标
  1. 如果当前相等或者模式串中字符为?,则移动相互的下标即可;
  2. 如果当前模式串字符为*,分别纪录lastmatch、laststar,并且移动模式串下标,但是不移动待匹配字符串下标,因为可能存在匹配0个字符串的情况;
  3. 如果当前相互对应的字符不再相等且不为*,如果前面有*号,说明之前的匹配失败了,模式字符串下标回到之前纪录laststar的后一位,不再移动,专门用来给待匹配字符串字符来匹配,这段时间内,si会不断的向前移动,直到匹配到相互的值相等才移动模式字符串的下标;
  4. 如果前面的情况都不符合,则肯定为False;

看看我的抽象派画风。

class Solution(object):
# p为匹配模式,s为字符串
def isMatch(self, s, p):
si, pi = 0, 0
lastmatch, laststar = -1, -1
sl, pl = len(s), len(p)
if pl - p.count('*') > sl:
return False
# 注意条件顺序
while si < sl:
if pi < pl and (s[si] == p[pi] or p[pi] == '?'):
pi += 1
si += 1
elif pi < pl and p[pi] == '*':
lastmatch, laststar = si, pi # 之所以不更新lastmatch是因为考虑到*只匹配0个字符串
pi += 1
# 再次进到这个判断,说明当前下标对应的值不相等
elif laststar != -1:
pi = laststar + 1 # pi当前不是*,并且回到上一次星的后面,专门用来给si匹配
lastmatch += 1 # 必须更新lastmatch,因为之前已经不想等,如果在回到开始的状态就会陷入死循环
si = lastmatch
else:
return False
# 可能存在p的末尾都是*的情况
while pi < len(p) and p[pi] == '*':
pi += 1
# 最后匹配成功模式字符串的下标必然为其长度,表示已经匹配完成
return pi == pl

tips:不要小看保存你的长度值,如果你频繁的用到的话,最好保存下来,比如在这里,我保存下来以后可以让我提升%10的beat submissions!

一样的原理,但是使用了递归的方式来做

class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
seen = {}
wild_single, wild_multi = "?", "*"
# seen has the pattern - source tuple as key, and bool result as success
source, pattern = s, p
def is_match(sindex, pindex):
key = (sindex, pindex)
if key in seen:
return seen[key]
result = True
# if there's no string, and pattern is not only * then fail
if sindex >= len(source):
for wildindex in xrange(pindex, len(pattern)):
if pattern[wildindex] != wild_multi:
result = False
break
# there's a string, but no pattern
elif pindex >= len(pattern):
result = False
# if next pattern is multi though, that's something
elif pattern[pindex] == wild_multi:
# for zero, simply check sindex, pindex + 1
result = is_match(sindex, pindex + 1) # just for easier debug
# if zero, than it's a match
# otherwise we need to check multi
# for that, if char is not a wild, then it has to match the source,
result = result or is_match(sindex + 1, pindex)
else:
# either a regular char, or wild_single
result = (( pattern[pindex] == wild_single or pattern[pindex] == source[sindex]) and
is_match(sindex + 1, pindex + 1))
seen[key] = result
return result
if (len(p) - p.count(wild_multi) > len(s)):
return False
return is_match(0, 0)

[LeetCode] Wildcard Matching 题解的更多相关文章

  1. LeetCode: Wildcard Matching 解题报告

    Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...

  2. [LeetCode] Wildcard Matching 外卡匹配

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

  3. [Leetcode] Wildcard Matching

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

  4. [leetcode]Wildcard Matching @ Python

    原题地址:https://oj.leetcode.com/problems/wildcard-matching/ 题意: Implement wildcard pattern matching wit ...

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

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

  6. [Leetcode] Wildcard matching 通配符匹配

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

  7. leetcode Wildcard Matching greedy algrithm

    The recursive program will result in TLE like this: class Solution { public: bool isMatch(const char ...

  8. [LeetCode]Wildcard Matching 通配符匹配(贪心)

    一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...

  9. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

随机推荐

  1. The dplyr package has been updated with new data manipulation commands for filters, joins and set operations.(转)

    dplyr 0.4.0 January 9, 2015 in Uncategorized I’m very pleased to announce that dplyr 0.4.0 is now av ...

  2. 6.类似Object监视器方法的Condition接口

    在<1.有关线程.并发的基本概念>中,我们利用synchronized关键字.Queue队列.以及Object监视器方法实现了生产者消费者,介绍了有关线程的一些基本概念.Object类提供 ...

  3. RabbitMQ 应用学习随笔

    1.安装 Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装RabbitMQ之前要先安装Erlang. erlang:http://www.erlang.org/download. ...

  4. 分布式锁2 Java非常用技术方案探讨之ZooKeeper

    前言:       由于在平时的工作中,线上服务器是分布式多台部署的,经常会面临解决分布式场景下数据一致性的问题,那么就要利用分布式锁来解决这些问题.以自己结合实际工作中的一些经验和网上看到的一些资料 ...

  5. JVM-2.Class文件结构

    1.Class文件 (1)无关性:除了平台无关性,JVM还支持语言无关性:目前Clojure.Groovy.JRuby.Jyphon.Scala等语言可以在JVM上运行.实现语言无关性的原理仍然是字节 ...

  6. vs2015未安装 Style 的 Visual Studio 语言支持

    解决方案:在浏览器搜索下载安装Microsoft ASP.NET and Web Tools即可 下载地址:https://www.microsoft.com/en-us/download/confi ...

  7. Innobackupex全备恢复(原理、演示)

    一.  Innobackupex恢复原理    After creating a backup, the data is not ready to be restored. There might b ...

  8. 用java来实现验证码功能。

    昨天在网上看到了一篇关于验证码的文章,很不错,但是有些不尽人意的地方,比如没有考虑到前端传过来的验证码如果是小写的话,那么做验证的时候就会出现错误, 因为java是严格区分大小写的,还有就是验证码会重 ...

  9. sublime 新手代码提示

    有提示的    你按   table   试试这就是按过的结果   是不是很方便这是按后的效果      是不是很方便 下面是各种简写效果html <html></html> ...

  10. javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印

    欢迎大家积极开心的加入讨论群 群号:371249677 (点击这里进群) javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置. ...