题目如下:

解题思路:本题和【leetcode】97. Interleaving String非常相似,同样可以采用动态规划的方法。记dp[i][j] = 1或者0 表示pattern[0:i]是否匹配string[0:j] ,如果pattern[i] == string[j] 或者 pattern[i] == '?',那么dp[i][j]  = dp[i-1][j-1];如果pattern[i] = '*' 则复杂一些,因为'*'可以匹配s[j-1],s[j] 或者不匹配,因此只要满足其中任意一种情况都视为匹配,得出递推关系式:dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])。

代码如下:

class Solution(object):
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
np = '' #合并连续出现的*,提高效率
for i in p:
if len(np) == 0:
np += i
elif np[-1] == i and i == '*':
continue
else:
np += i #pattern和string都加上'#'开头,处理pattern以*开头,但是又不做匹配的情况
np = '#' + np
p = np
s = '#' + s dp = [[0] * len(s) for i in p]
#print dp dp[0][0] = 1 for i in range(len(dp)):
for j in range(len(dp[i])):
if p[i] == '*':
if i > 0 and j > 0:
dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])
elif i > 0 and j == 0:
dp[i][j] = dp[i-1][j]
elif i == 0 and j > 0:
dp[i][j] = dp[i][j-1]
elif i > 0 and j > 0 and (p[i] == '?' or p[i] == s[j]):
dp[i][j] = dp[i-1][j-1]
#print dp
return dp[-1][-1] == 1

【leetcode】44. Wildcard Matching的更多相关文章

  1. 【LeetCode】44. Wildcard Matching (2 solutions)

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

  2. 【一天一道LeetCode】#44. Wildcard Matching

    一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...

  3. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  4. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  5. 【LeetCode】1023. Camelcase Matching 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+字典 日期 题目地址:https://leet ...

  6. 【LEETCODE】44、509. Fibonacci Number

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. 【leetcode】1023. Camelcase Matching

    题目如下: A query word matches a given pattern if we can insert lowercaseletters to the pattern word so ...

  8. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. LeetCode - 44. Wildcard Matching

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

随机推荐

  1. 170814关于Cookie的知识点

    1.会话控制 Http协议   Http协议两个缺陷: 1.HTTP协议是纯文本的    2.HTTP协议是无状态的 服务器不能简单的通过HTTP协议来区分多次请求是否发自同一个用户    虽然通过H ...

  2. p5342 [TJOI2019]甲苯先生的线段树

    分析  代码 #include<bits/stdc++.h> using namespace std; #define int long long ],yy[],cnt1,cnt2; ][ ...

  3. delphi 牛逼 了 app (已在软件界掀起波澜)10分钟10行代码做出让人惊叹的程序

    (已在软件界掀起波澜)10分钟10行代码做出让人惊叹的程序 http://v.qq.com/x/page/m0328h73bs7.html?ptag=bbs_csdn_net

  4. Vagrant 手册之 Vagrantfile - 配置版本

    原文地址 配置版本是 Vagrant 1.1+(引入了大量新功能和配置选项) 能够与 Vagrant 1.0.x Vagrantfiles 保持向后兼容的机制. 现在运行 vagrant init 时 ...

  5. jQuery DataTables 分页

    HTML:================================================================== <div class="ibox-con ...

  6. eclipse配置,快捷键备忘

    1. General --> Workspace --> UTF-82. General --> Editors --> Associations --> JSP --& ...

  7. Invalid column name on sql server update after column create

    问题:新建一个测试表xx as code into xx select * from xx 给这个表添加一个列val, val列不允许为空,将表中已有的数据val值更新为1 alter table x ...

  8. 11、numpy——字符串函数

    NumPy 字符串函数 以下函数用于对 dtype 为 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作. 它们基于 Python 内置库中的标准字符串函数. ...

  9. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  10. Helm教程

    1.概述 Helm是k8s的包管理工具,类似Linux系统常用的 apt.yum等包管理工具. 使用helm可以简化k8s应用部署 2.基本概念 Chart:一个 Helm 包,其中包含了运行一个应用 ...