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. ...
随机推荐
- U盘永久系统-centos
U盘永久系统-centos 问题: 服务器centos系统崩溃,重装需要备份其中数据,约4T,实验室有远程存储服务器,然而rescue模式进去后无法挂载远程存储,只好做一个真正的U盘系统解决了. 方案 ...
- Mac环境下终端(Terminal)用ssh 连接服务器问题 Received disconnect from 120.55.x.x: 2: Too many authentication failures for root
由于这台Mac配置git生成公钥后,ssh连接就出现来这个问题 Received disconnect from 120.55.x.x: 2: Too many authentication fail ...
- MVC自定义错误页面
MVC异常处理主要有三种方案:1.基于HandleErrorAttribute重写OnException方法:2.基于Global.apsx添加Application_Error方法:3.直接在Web ...
- Linnx 服务器中mysql 无法正常访问问题
本机连接远程Linnx服务器不通 1. 检测防火墙 -- 保证防火墙关闭 查看到iptables服务的当前状态:service iptables status. 但是即使服务运行了,防火墙也不一定起作 ...
- 【转】使用JMeter对数据库做压力测试
作为一名开发人员,大多情况下都会认真的做好功能测试,但是却常常忽略了软件开发之后的压力测试,尤其是在面向大量用户同时使用的Web应用系统的开发过程,压力测试往往是不够充分的.近期我在一个求职招聘型的网 ...
- Java-Runoob:Java Stream、File、IO
ylbtech-Java-Runoob:Java Stream.File.IO 1.返回顶部 1. Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出 ...
- 十九 yield方法
yield()方法的作用是放弃当前的CPU资源,让 其他任务占用CPU执行时间.但放弃时间不确定, 有可能刚刚放弃,马上又获得CPU时间片.
- OD 实验(十九) - 对多态和变形程序的逆向
程序: 这个窗口显示这是一个需要去除的 Nag 窗口 点击“确定” 用 PEiD 看一下 这是一个用汇编语言写的程序 逆向: 用 OD 载入程序 Nag 窗口的标题和文本 右键 -> 查找 -& ...
- python开发_python中的变量:全局变量和局部变量
如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...
- CentOS7.6安装Maven
官网下载地址:http://maven.apache.org/download.cgi 第一步:软件下载安装 进行安装目录:cd /opt/software (如果目录不存在,请先创建目录) 下载二进 ...