[抄题]:

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 *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "*"
Output: true
Explanation: '*' matches any sequence.

Example 3:

Input:
s = "cb"
p = "?a"
Output: false
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.

Example 4:

Input:
s = "adceb"
p = "*a*b"
Output: true
Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".

Example 5:

Input:
s = "acdcb"
p = "a*c?b"
Output: false

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

初始化dp[0][j]的时候,除开一直是*的情况,都不能随意匹配

[思维问题]:

搞不清楚和第十题的区别:就是能不能遗传 dp[i][j] = dp[i][j - 2]就行了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. dp[i][j - 1] 是重复一个字符的情况(其中包括了空字符),所以空字符不需要单独列出来

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. dp[i][j - 1] 是重复一个字符的情况(其中包括了空字符),所以空字符不需要单独列出来

[复杂度]:Time complexity: O(mn) Space complexity: O(mn)

[算法思想:递归/分治/贪心]:贪心

[关键模板化代码]:

for (int i = 1; i <= m; i++) {
dp[i][0] = false;
} for (int j = 1; j <= n; j++) {
if (p.charAt(j - 1) == '*') dp[0][j] = true;
else break;
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

10有前序的

[代码风格] :

[是否头一次写此类driver funcion的代码] :

class Solution {
public boolean isMatch(String s, String p) {
//ini: dp[][]
int m = s.length();
int n = p.length();
boolean[][] dp = new boolean[m + 1][n + 1]; //cc: dp[0][0], dp[0][], dp[][0]
dp[0][0] = true; for (int i = 1; i <= m; i++) {
dp[i][0] = false;
} for (int j = 1; j <= n; j++) {
if (p.charAt(j - 1) == '*') dp[0][j] = true;
else break;
} //for loop: not * must equal or ., * 0 or more
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++)
if (p.charAt(j - 1) != '*') {
//1s
dp[i][j] = dp[i - 1][j - 1] && (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '?');
}else {
//multiple s, 0 s
dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
}
} //return m n
return dp[m][n];
}
}

44. Wildcard Matching 有简写的字符串匹配的更多相关文章

  1. 44. Wildcard Matching

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

  2. LeetCode - 44. Wildcard Matching

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

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

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

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

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

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

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

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

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

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

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

  8. [leetcode]44. Wildcard Matching万能符匹配

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

  9. 44. Wildcard Matching (String; DP, Back-Track)

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

随机推荐

  1. Android.mk语法解析

    Android.mk 相当于 Linux 中的 Makefile 文件,用来向安卓系统描述如何编译源代码.该文件会被编译器解析多次,所以尽量减少在 Android.mk 中声明变量. Android. ...

  2. 常见企业IT支撑【2、samba共享文件夹】

    samba共享文件夹,较Window自带的比较:开源,安全 建议安装samba4,兼容性好 1.安装samba #yum -y install samba4 samba4-client 2.备份sam ...

  3. ora-01658: 无法为表空间*****中的段创建 INITIAL 区

    ora-01658: 无法为表空间*****中的段创建 INITIAL 区 原因:表空间不足 增大表空间.如果表空间动态增长,则扩大表空间的最大值.

  4. Markdown初步使用

    一.兼容 HTML Markdown 的理念是,能让文档更容易读.写和随意改.HTML 是一种发布的格式,Markdown 是一种书写的格式.就这样,Markdown 的格式语法只涵盖纯文本可以涵盖的 ...

  5. "锁"

    “锁”,指的是状态切换,状态未切换完成,加上锁,完成后才打开锁. 下面例子要完成一个点击按钮切换颜色的小示例,先看未加“锁”时候的效果 <!DOCTYPE html> <html l ...

  6. locale错误导致Java中文乱码错误的总结

    线上执行MapReduce任务计算时,经过排查发现了某些服务器计算的数据出现中文乱码问题,但是服务器的配置是完全一致的.由于我们使用的key可能包含中文,中文乱码问题体现在每次合并map记录的时候计算 ...

  7. 非常漂亮js动态球型云标签特效代码

    <%@ page contentType="text/html;charset=UTF-8" language="java" import="j ...

  8. 浅谈PHP面向对象编程(五、继承)

    5.0 继承 5.1 继承的概念 在现实生活中,继承一般指的是子女继承父辈的财产.在程序中,继承描述的是事物之间的所属关系,通过继承可以使许多事物之间形成一种关系体系 . 例如猫和狗都属于动物,程序中 ...

  9. PHP程序连接多个redis实例做缓存

    1.redis配置: $CONFIG_REDIS = array(     array('host' => '192.168.19.29', 'port' => '6379', 'dbIn ...

  10. 什么是事件代理?DOM2.0标准事件模型的三个阶段

    体验更优排版请移步原文:http://blog.kwin.wang/programming/js-event-delegation.html 事件代理,又称事件委托(Delegation),就是将处理 ...