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. Visual Studio生成webservice代理类

    首先点击 vs菜单栏->工具 ,选择 外部工具, 在弹出的窗口中点击 添加, 然后在“标题”行中输入"WSDL生成代理类", "命令"行中输入" ...

  2. mongodb集群配置副本集

    测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 mongo01 从服务器IP地址:192.168.197.22 mongo02 从服务器IP地址: ...

  3. [ZZ] UIUC同学Jia-Bin Huang收集的计算机视觉代码合集

    UIUC同学Jia-Bin Huang收集的计算机视觉代码合集 http://blog.sina.com.cn/s/blog_4a1853330100zwgm.htmlv UIUC的Jia-Bin H ...

  4. 第一天Python

    一.开发语言 高级语言:Python  Java.PHP     高级语言--字节码(PHP适用于写网页) 低级语言:C.汇编--机器码(底层开发,根本,效率低) 二.Python种类 三.安装

  5. SWD通讯

    这几日看到坛里有几个关于SWD协议相关的文章,自己也尝试了下,有点体会,也有些疑惑,写出来与大家分享和交流下.    以下我的模拟SWD接口的板子简称为Host,目标MCU(即我要连接的板子)简称为T ...

  6. python之路——16

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 学习内容 1.内置函数 1. python 数据类型:int bool 数据结构:dic list tupl ...

  7. normalization正规化

    用到sklearn模块 from sklearn import preprocessing用preprocessing.scale正规化 print(preprocessing.scale(a))

  8. python中建模分析零息票收益率曲线--复利和连续复利

    收益率曲线(Yield Curve)是显示一组货币和信贷风险均相同,但期限不同的债券或其他金融工具收益率的图表.纵轴代表收益率,横轴则是距离到期的时间.在此用python建模分析零息票收益率曲线,输出 ...

  9. Tomcat的目录结构详细介绍(超全)

    打开tomcat的解压之后的目录可以看到如下的目录结构:  1.bin: bin目录主要是用来存放tomcat的命令,主要有两大类,一类是以.sh结尾的(linux命令),另一类是以.bat结尾的(w ...

  10. 事务、事务特性、事务隔离级别、spring事务传播特性

    事务.事务特性.事务隔离级别.spring事务传播特性   1.什么是事务: 事务是程序中一系列严密的操作,所有操作执行必须成功完成,否则在每个操作所做的更改将会被撤销,这也是事务的原子性(要么成功, ...