Given an input string (s) and a pattern (p), 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).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like ? or *.
Input:
s = "adceb"
p = "*a*b"
Output: true

题意:

' ?' any single character.
' * ' 0 or more sequence(s).
whether p can match s ?

    这题的 ' * '可以像wildcard万能卡一样,代表任意 0个或多个子序列

思路:

跟 10. Regular Expression Matching 的思路很像, 不同的是 ' * ' 在 10. Regular Expression Matching  是依赖于之前的元素。而 ' * '在本题中就直接是任意子序列。

二维dp

  p =     0  *  a  * b
0 T
s = "a
d
c
e
b"

初始化,

dp[0][0] = true

是否需要预处理第一个row: dp[0][j], 发现当S为空,P为'*' 时,P若取0个subsequnce就可能变成空,此时两个字符串match。需要预处理。

是否需要预处理第一个col:dp[i][0],  发现当P为空,S为任意字符时,肯定不match。不需要预处理,因为默认default就是false。

转移方程,

若两个字符串当前的char相同:

p.charAt(j-1) == s.charAt(i-1) or  p.charAt(j-1) == ' ?'  则当前字符match, 那么dp[i][j] 的结果可以直接拿dp[i-1][j-1]的取值

若两个字符串当前的char不同:

1.  p.charAt(j-1) == '*' 时,先退后一步先去check一下T/F。若 "*" 可以代表0个subsequnce,dp[i][j] = dp[i][j-1]

2.  p.charAt(j-1) == '*' 时,若'*'代表的1 or more subsequnce, 则dp[i][j] = dp[i-1][j] 即S: "abcd" 和 P:"ab * " 是否match, 返回去看 S: "abc"  和  P:"ab * " 是否match

代码:

 class Solution {
public boolean isMatch(String s, String p) {
boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
dp[0][0] = true;
for(int j = 1; j <= p.length(); j++){
if(p.charAt(j-1) == '*'){
dp[0][j] = dp[0][j-1];
}
} for(int i = 1; i <= s.length() ; i++){
for(int j = 1; j <= p.length(); j++){
if(p.charAt(j-1) == s.charAt(i-1) || p.charAt(j-1) == '?' ){
dp[i][j] = dp[i-1][j-1];
}else{
if (p.charAt(j-1) == '*'){
dp[i][j] = dp[i-1][j] || dp[i][j-1];
}
}
}
}
return dp[s.length()][p.length()];
}
}

[leetcode]44. Wildcard Matching万能符匹配的更多相关文章

  1. leetcode 44. Wildcard Matching(模糊匹配)

    搬运工了- - https://blog.csdn.net/jmspan/article/details/51460021

  2. LeetCode - 44. Wildcard Matching

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

  3. 第八周 Leetcode 44. Wildcard Matching 水题 (HARD)

    Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分 ...

  4. [LeetCode] 44. Wildcard Matching 外卡匹配

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  5. LeetCode 44 Wildcard Matching(字符串匹配问题)

    题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description   '?' Matches any single chara ...

  6. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  7. 44. Wildcard Matching

    题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...

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

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

  9. 【leetcode】Wildcard Matching

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

随机推荐

  1. PythonStudy——文件操作 File operation

    # 文件:就是硬盘的一块存储空间 # 1.使用文件的三步骤: # 打开文件- 得到文件对象:找到数据存放在硬盘的位置,让操作系统持有该空间,具有操作权# 硬盘空间 被 操作系统持有# 文件对象f 被 ...

  2. Hi3516EV100烧录出厂固件

    1.Hitool烧录uboot 2.uboot下烧录固件 setenv serverip 192.168.1.138 mw.b ff ;tftp ;sf probe ;sf erase ;sf wri ...

  3. 《Linux内核原理与分析》第四次作业

    跟踪分析Linux内核的启动过程 使用实验楼的虚拟机打开shell 使用 gdb 跟踪调试内核 使用 qemu qemu -kernel linux-3.18.6 /arch/x86/boot/baI ...

  4. spring aop无法拦截类内部的方法调用

    1.概念 拦截器的实现原理就是动态代理,实现AOP机制.Spring 的代理实现有两种:一是基于 JDK Dynamic Proxy 技术而实现的:二是基于 CGLIB 技术而实现的.如果目标对象实现 ...

  5. 从头调试stm32 HID

    目录: 第1部分:参照“正点原子USB虚拟串口工程移植步骤”移植ST的USB HID工程(失败了): 第2部分:在1的基础上,替换USB HID初始化代码为ST 例程中的代码,编译后根据报错调试(失败 ...

  6. WPF 选项卡

    1.引用 xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock" 2.xaml代码 <xcad:DockingMa ...

  7. 使用Mechanize实现自动化表单处理

    使用Mechanize实现自动化表单处理   mechanize是对urllib2的部分功能的替换,能够更好的模拟浏览器行为,在web访问控制方面做得更全面 mechanize的特点: 1 http, ...

  8. 第18课 类型萃取(2)_获取返回值类型的traits

    1. 获取可调用对象返回类型 (1)decltype:获取变量或表达式的类型(见第2课) (2)declval及原型 ①原型:template<class T> T&& d ...

  9. You Only Look Once: Unified, Real-Time Object Detection

    论文下载:http://arxiv.org/abs/1506.02640  代码下载:https://github.com/pjreddie/darknet Abstract 作者提出一种新的目标检测 ...

  10. js解决转义字符问题

    数据“\\s=7\\c=1\\j=1\\p=1”, 转义出来变成“\s=7\c=1\j=1\p=1” 结果:可以这样转换str=str.replace(/\\/g,'\\\\');