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:
scould be empty and contains only lowercase lettersa-z.pcould 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. ...
随机推荐
- 使用 Excel 可以很方便的做程序原型
使用 Excel 可以很方便的做程序原型 比如计算 单片机的端口模式,可以使用 Excel 很方便的计算出来,花了 15 分钟做好. 还可以使用函数自动根据二进制计算出 十六进制. 然后如果再使用软件 ...
- npm dose not support Node.js v10.15.3
事件起因: 楼主在vue-cli官网,尝试使用vue-cli3脚手架+yarn包管理器构建项目时,命令行窗口提示node版本不对.如下图 这个大家都知道该如何去解决,直接去node官网下载符合版本的n ...
- 什么是spark(六)Spark中的对象
Spark中的对象 Spark的Conf,极简化的场景,可以设置一个空conf给sparkContext,在执行spark-submit的时候,系统会默认给sparkContext赋一个SparkCo ...
- 【转】【备忘录】MySQL性能优化的21个最佳实践 和 mysql使用索引
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我 们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数 ...
- DS05--查找
一.学习总结 1.查找的思维导图 2.查找学习体会 2.1 关联容器和顺序容器 c++中有两种类型的容器:顺序容器和关联容器,顺序容器主要有:vector.list.deque等.其中vector表示 ...
- 用活firewalld防火墙之service
原文地址:http://www.excelib.com/article/291/show 前面学生已经给大家介绍了在firewalld中service的概念以及在zone中怎么使用service,但是 ...
- 20165226 MySort的实现
MySort的实现 一.实验要求 研究sort的其他功能,要能改的动代码,模拟实现Linux下Sort -t : -k 2的功能. 二.代码 /** * Created by xiang on 201 ...
- Java Array 方法和使用
1.Arrays.toString():数组转字符串 格式:Arrays.toString(数组名) 将数组转化成字符串,此时输出的结果是字符串类型. import java.util.Arrays; ...
- [python] 使用scikit-learn工具计算文本TF-IDF值
在文本聚类.文本分类或者比较两个文档相似程度过程中,可能会涉及到TF-IDF值的计算.这里主要讲述基于Python的机器学习模块和开源工具:scikit-learn. 希望文章对你有所帮 ...
- node中express的中间件之basicAuth
basicAuth中间件为网站添加身份认证功能.在使用了该中间件后, 用户访问网站时必须输入用户名与密码,在用户输入了用户名与密码并通过验证之后才能访问网站. 当用户输入的用户名和密码符合条件,中间件 ...