44. Wildcard Matching 有简写的字符串匹配
[抄题]:
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 lettersa-z
.p
could be empty and contains only lowercase lettersa-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]就行了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- dp[i][j - 1] 是重复一个字符的情况(其中包括了空字符),所以空字符不需要单独列出来
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 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 有简写的字符串匹配的更多相关文章
- 44. Wildcard Matching
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- LeetCode 44 Wildcard Matching(字符串匹配问题)
题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single chara ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 第八周 Leetcode 44. Wildcard Matching 水题 (HARD)
Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分 ...
- [LeetCode] 44. Wildcard Matching 外卡匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- [leetcode]44. Wildcard Matching万能符匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- 44. Wildcard Matching (String; DP, Back-Track)
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
随机推荐
- 互联网的keyvalue处理
今天在和许伟讨论系统配置页面得时候,许伟提到了“打通页面”的概念,当时我没太明白,后来才知道是指类似于cloudera里面的配置页面那种,不是列表页,而是展示+编辑在一个页面.刚才想了一下,其实对于这 ...
- 军哥LNMP优化
http://bbs.vpser.net/thread-8914-1-1.html http://www.zxsdw.com/index.php/archives/881/ 修改/usr/local/ ...
- 5.JMeter测试mysql数据库
1.使用jmeter测试mysql数据库时,需要导入jar包,jar包网盘地址为:链接: https://pan.baidu.com/s/1-5-s7HccudT4GirpmBVn6Q 密码: bea ...
- Unit03: 容器对路径的处理 、 Servlet特性
Unit03: 容器对路径的处理 . Servlet特性 案例一:查询,增加员工: 重定向 处理请求资源路径 目录结构: 案例代码: package dao; import java.io.Seria ...
- C++直接初始化和复制初始化2
现在正式对C++中对象建立和初始化做一个总结. (1)复制初始化的基本原理 我们知道,对象在内存中的直接表象是在内存中占有一个一定大小的空间.分配空间是建立对象的第一步.但是刚刚分配的空间就像一个没有 ...
- PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- [Java]基础.端口
Map<String,String[]> map = request.getParameterMap(); BeanUtils.populate(user,map); // 遍历 use ...
- Java 中的instanceof 运算符
Java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. 用法:resu ...
- 一种SequenceFile的格式研究
最近仔细研究了以下公司中使用的SequenceFile文件格式,SequenceFile的格式比较紧凑,实现了从中间读取文件内容(便于hadoop将文件进行适当地切分),同时也可以支持仅读取文件的元数 ...
- Java多线程面试问题集锦
参看:http://www.importnew.com/1428.html