【题目

匹配通配符*,?,DP动态规划,重点是*的两种情况

想象成两个S、P长度的字符串,P匹配S。

S中不会出现通配符。

【条件】

(1)P=null,S=null,TRUE

(2)P=null,S!=null,P必然无法匹配S,FALSE。

(3)P[i]=“*” 的TRUE/FALSE状态等价于P[i-1]

(4)考虑*两种情况,ab, ab*(*=null)、abcd, ab*(*=cd)

【参考】

The most confusing part for me is how to deal with '*'. At first I couldn't figure out why the condition would be (dp[i-1][j] == true || dp[i][j-1] == true). Hope detailed DP description below helps!

  • dp[i][j]: true if the first i char in String s matches the first j chars in String p
  • Base case:
    • origin: dp[0][0]: they do match, so dp[0][0] = true
    • first row: dp[0][j]: except for String p starts with *, otherwise all false
    • first col: dp[i][0]: can't match when p is empty. All false.
  • Recursion:
    • Iterate through every dp[i][j]
    • dp[i][j] = true:
      • if (s[ith] == p[jth] || p[jth] == '?') && dp[i-1][j-1] == true
      • elif p[jth] == '*' && (dp[i-1][j] == true || dp[i][j-1] == true) 
        -for dp[i-1][j], means that * acts like an empty sequence. 
        eg: ab, ab*
        -for dp[i][j-1], means that * acts like any sequences
        eg: abcd, ab*
  • Start from 0 to len
  • Output put should be dp[s.len][p.len], referring to the whole s matches the whole p

Be careful about the difference of index i,j in String (0 to len-1) and the index i, j in dp (0 to len)!


Below is my AC code in Java:

public boolean isMatch(String s, String p) {
if(s == null || p == null) return false;
int sLen = s.length();
int pLen = p.length();
boolean[][] dp = new boolean[sLen + 1][pLen + 1]; // Base cases:
dp[0][0] = true;
for(int i = 1; i <= sLen; i++){
dp[i][0] = false;
}
for(int j = 1; j <= pLen; j++){
if(p.charAt(j-1) == '*'){
dp[0][j] = dp[0][j-1];
}
} // Recursion:
for(int i = 1; i <= sLen; i++){
for(int j = 1; j <= pLen; j++){
if((s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1) == '?') && dp[i-1][j-1])
dp[i][j] = true;
else if (p.charAt(j-1) == '*' && (dp[i-1][j] || dp[i][j-1]))
dp[i][j] = true;
}
}
return dp[sLen][pLen];
}

[Leetcode 44]通配符匹配Wildcard Matching的更多相关文章

  1. Java实现 LeetCode 44 通配符匹配

    44. 通配符匹配 给定一个字符串 (s) 和一个字符模式 § ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字 ...

  2. [leetcode] 44. 通配符匹配(Java)(动态规划)

    44. 通配符匹配 动态规划 做动态规划很简单,三步走: 第一步,判断可否用动态规划做,即判断是否满足两个条件:①最优子结构,②重叠子问题.显然该题求s与p是否match,可由其字串层层分解上来. 我 ...

  3. LeetCode 44. 通配符匹配(Wildcard Matching)

    题目描述 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完 ...

  4. [Swift]LeetCode44. 通配符匹配 | Wildcard Matching

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

  5. leetcode 第43题 Wildcard Matching

    题目:(这题好难.题目意思类似于第十题,只是这里的*就是可以匹配任意长度串,也就是第十题的‘.*’)'?' Matches any single character. '*' Matches any ...

  6. leetcode 44 字符匹配

    题意:s是空串或包含a-z字母: p为包含a-z字母或?或 * (其中*可以匹配任意字符串包括空串,?可以匹配任意字符). 思路: 1)特殊情况:当s为空串时,p为连续 * 时,则连续 * 的位置都为 ...

  7. Leetcode44. 通配符匹配(动态规划)

    44. 通配符匹配 动态规划 \(f_{i,j}\)为\(s\)匹配\(i\),\(t\)匹配\(j\)是否成功 贪心 相比之下这个思维性更强 考虑两个*,两个星号间的过渡,只需要过渡完到第二个星号, ...

  8. [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching

    开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...

  9. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

随机推荐

  1. HAL无阻塞延时

    //实现间隔time_interval时间点亮红灯(此时间间隔并不是绝对的,是大于等于的关系)//用于系统要求无延时且延时时间粗略的场合,比如间隔一段时间采样数据,间隔一段时间点亮状态灯等//HAL_ ...

  2. (转)C# 单例模式

    文章1:  一.多线程不安全方式实现 public sealed class SingleInstance    {        private static SingleInstance inst ...

  3. 在Winfrom 中,如何实现combox 的列表自动显示ToolTip提示 ?

    //带ToolTip的combox类文件 public class ComboBoxWithTooltip : ComboBox { //tipProperty为显示ToolTip文本的数据源的属性 ...

  4. android --------学习流程图

    如何快速入门和进阶安卓开发,是很多技术小白的疑问. 大家都知道,Android开发要学的技能非常多,技术更新速度还快,但是总的来说:掌握最核心的技术,最规范的开发流程,成为专业.出色的安卓开发工程师也 ...

  5. Spring Batch 基本的批处理指导原则

    下面是一些关键的指导原则,可以在构批量处理解决方案可以参考: 请记住,通常皮脸处理体系结构将会影响在线应用的体系结构,同时反过来也是一样的.在你为批量任务和在线应用进行设计架构和环境的时候请尽可能的使 ...

  6. [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)

    描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...

  7. 2 爬虫 requests模块

    requests模块 Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,Requests它会比urllib更加方便,reques ...

  8. Sphinx实时索引

    数据库中的数据很大,然后我有些新的数据后来加入到数据库中,也希望能够检索到,全部重新建立索引很消耗资源,这样需要用到“主索引+增量索引”的思路来解决,这个模式实现的基本原理是设置两个数据源和两个索引. ...

  9. Python安装第三方库,报错超时: Read timed out.

    1.安装beautifulsoup4 >pip install beautifulsoup4 报错超时: Read timed out. 2.解决办法:pip --default-timeout ...

  10. PAT 1003 Emergency

    1003 Emergency (25 分)   As an emergency rescue team leader of a city, you are given a special map of ...