题目如下:

解题思路:本题和【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])。

代码如下:

  1. class Solution(object):
  2. def isMatch(self, s, p):
  3. """
  4. :type s: str
  5. :type p: str
  6. :rtype: bool
  7. """
  8. np = ''
  9.  
  10. #合并连续出现的*,提高效率
  11. for i in p:
  12. if len(np) == 0:
  13. np += i
  14. elif np[-1] == i and i == '*':
  15. continue
  16. else:
  17. np += i
  18.  
  19. #pattern和string都加上'#'开头,处理pattern以*开头,但是又不做匹配的情况
  20. np = '#' + np
  21. p = np
  22. s = '#' + s
  23.  
  24. dp = [[0] * len(s) for i in p]
  25. #print dp
  26.  
  27. dp[0][0] = 1
  28.  
  29. for i in range(len(dp)):
  30. for j in range(len(dp[i])):
  31. if p[i] == '*':
  32. if i > 0 and j > 0:
  33. dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])
  34. elif i > 0 and j == 0:
  35. dp[i][j] = dp[i-1][j]
  36. elif i == 0 and j > 0:
  37. dp[i][j] = dp[i][j-1]
  38. elif i > 0 and j > 0 and (p[i] == '?' or p[i] == s[j]):
  39. dp[i][j] = dp[i-1][j-1]
  40. #print dp
  41. 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. 去掉注释index.html

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  2. UVA 11752 The Super Powers(暴力)

    题目:https://cn.vjudge.net/problem/UVA-11752 题解:这里只讨论处理越界的问题. 因为题目最上界是 264-1. 我们又是求次幂的. 所以当我们就可以知道 i 的 ...

  3. 设置VsCode自动换行

    方法如下: 文件 -> 首选项 -> 设置 如果你是Mac则是右上角 Code -> 首选项 -> 设置 然后在右侧的编辑窗口中添加 1 "editor.wordWr ...

  4. STM32 ADC基础与多通道采样

    12位ADC是一种逐次逼近型模拟数字数字转换器.它有多达18个通道,可测量16个外部和2个内部信号源.ADC的输入时钟不得超过14MHZ,它是由PCLK2经分频产生.如果被ADC转换的模拟电压低于低阀 ...

  5. http代理工具delphi源码

    http://www.caihongnet.com/content/xingyexinwen/2013/0721/730.html http代理工具delphi源码 以下代码在 DELPHI7+IND ...

  6. C# 做延迟,但系统又能同时能执行其它任务

    private void Delay(int Millisecond) //使用时直接调用即可 { DateTime current = DateTime.Now; while (current.Ad ...

  7. Easyui表格显示日期格式错误

    问题   如下图: 解决办法: 以Sql Server 2008为例,在执行查询时,把“采购日期”字段由datetime转换为string,前端以string类型显示.

  8. Java8默认方法

    Java8引入的接口默认方法实现一个新的概念.此功能是为了向后兼容性增加,使旧接口可用于利用JAVA8. lambda表达式的能力,例如,列表或集合接口不具备forEach方法声明.从而增加了这样的方 ...

  9. php开发环境是什么

    软件开发环境(Software Development Environment,SDE)是指在基本硬件和数字软件的基础上,为支持系统软件和应用软件的工程化开发和维护而使用的一组软件,简称SDE.它由软 ...

  10. 如何配置JedisPool的参数

    转自:http://blog.csdn.net/huahuagongzi99999/article/details/13631579 如何配置Pool的参数 JedisPool的配置参数很大程度上依赖 ...