【题目

匹配通配符*,?,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. 第 6 章 存储 - 042 - 用 volume container 共享数据

    volume container volume container 是专门为其他容器提供 volume 的容器.它提供的卷可以是 bind mount,也可以是 docker managed volu ...

  2. Mongodb 分享(一)

    Mongodb使用基础知识: 一.简介 1.mongodb是什么? 1)MongoDB 是一个基于分布式文件存储的数据库.由 )mongodb 客户端:NoSQL Manager for MongoD ...

  3. SQL语句内做除法得出百分比

    保留两位小数点 SELECT ROUND(CAST(field1 AS DOUBLE)/field2, 2) * 100 FROM TB; 不保留 SELECT CAST(field1 AS FLOA ...

  4. 无法访问SVN历史记录的问题

      今天在eclipse中发现无法访问SVN的历史记录,提示条目不可读,截图如下: 用小乌龟客户端试了试也不行,截图如下: 最后解决办法是在SVN服务器上将代码仓库中conf目录下的svnserve. ...

  5. 20165303学习基础和C语言基础调查

    20165303学习基础和C语言基础调查 技能学习心得 我认为我的乒乓球打的还不错,不能说非常好,但是基本的一些技巧都还是会的,小时候爸爸就非常爱看乒乓球比赛,有时候也带着我一起看,最开始看的时候我发 ...

  6. Spring Boot之logback日志最佳实践

    一.Spring Boot日志介绍 Spring Boot对所有内部日志记录使用了Commons Logging,但是底层日志实现是开放的.为Java Util日志记录.Log4J2和Logback提 ...

  7. codeforces668b //Little Artem and Dance// Codeforces Round #348

    题意:2种操作,转动或者奇偶位互换. 不论怎么交换,1的后两位一定是3,3的后两位一定是5.因此只要记录1,2的位置. //#pragma comment(linker,"/STACK:10 ...

  8. android -------- Data Binding的使用 ( 五) include

    Data Binding的中 include 标签的使用 inclune使用和原来一样,但要如何使数据也在 include中使用呢? 先看看我的布局文件 include的布局文件,也要使用 <l ...

  9. Arthur and Brackets CodeForces - 508E (贪心,括号匹配)

    大意: n个括号, 位置未知, 给出每对括号左右间距, 求输出一个合法括号序列. 最后一个括号间距一定为1, 从右往左遍历, 每次括号划分越小越好. #include <iostream> ...

  10. dp练习2

    1, CF 808G Anthem of Berland 2, CF 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses