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. 登录ssh提示:ssh_exchange_identification: read: Connection reset by peer error

    vim /etc/hosts.allow 添加 sshd : ALL

  2. java:try...catch...finally

    try...catch...finally 规则: 可以没有 finally 块 如果没有 catch 块,则必须跟一个 finally 块 当在 try 块或 catch 块中遇到 return 语 ...

  3. 从头调试stm32 HID

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

  4. PAT 甲级 1041 Be Unique (20 分)

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

  5. 使用Tensorflow操作MNIST数据

    MNIST是一个非常有名的手写体数字识别数据集,在很多资料中,这个数据集都会被用作深度学习的入门样例.而TensorFlow的封装让使用MNIST数据集变得更加方便.MNIST数据集是NIST数据集的 ...

  6. 获得驱动器信息卷设备&&Ring3得到磁盘文件系统(NTFS WIN10)

    // GetLogicalDriveStrings.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows ...

  7. 使用Oracle DBLink进行数据库之间对象的访问操作

    Oracle中自带了DBLink功能,它的作用是将多个oracle数据库逻辑上看成一个数据库,也就是说在一个数据库中可以操作另一个数据库中的对象,例如我们新建了一个数据database1,我们需要操作 ...

  8. 黑色背景下 vs把{}括号变黑问题

    最近喜欢把VS的背景搞成黑色,据说这样可以对眼睛好一点,然后就蛋疼的碰到点击括号中的内容,括号就变黑的问题,这样黑色背景下就什么看不到了. 原因是装了番茄助手导致的,解决办法,把选中行VA Brace ...

  9. 异常处理,MD5

    异常处理. try except raise try: 代码 except 异常类: 除了错, 如何处理异常 except 异常类: 除了错, 如何处理异常 except 异常类: 除了错, 如何处理 ...

  10. i++ 是线程安全的吗?

    相信很多中高级的 Java 面试者都遇到过这个问题,很多对这个不是很清楚的肯定是一脸蒙逼.内心肯定还在质疑,i++ 居然还有线程安全问题?只能说自己了解的不够多,自己的水平有限. 先来看下面的示例来验 ...