[leetcode]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*
.
Input:
s = "adceb"
p = "*a*b"
Output: true
题意:
' ?' any single character.
' * ' 0 or more sequence(s).
whether p can match s ?
这题的 ' * '可以像wildcard万能卡一样,代表任意 0个或多个子序列
思路:
跟 10. Regular Expression Matching 的思路很像, 不同的是 ' * ' 在 10. Regular Expression Matching 是依赖于之前的元素。而 ' * '在本题中就直接是任意子序列。
二维dp
p = 0 * a * b
0 T
s = "a
d
c
e
b"
初始化,
dp[0][0] = true
是否需要预处理第一个row: dp[0][j], 发现当S为空,P为'*' 时,P若取0个subsequnce就可能变成空,此时两个字符串match。需要预处理。
是否需要预处理第一个col:dp[i][0], 发现当P为空,S为任意字符时,肯定不match。不需要预处理,因为默认default就是false。
转移方程,
若两个字符串当前的char相同:
p.charAt(j-1) == s.charAt(i-1) or p.charAt(j-1) == ' ?' 则当前字符match, 那么dp[i][j] 的结果可以直接拿dp[i-1][j-1]的取值
若两个字符串当前的char不同:
1. p.charAt(j-1) == '*' 时,先退后一步先去check一下T/F。若 "*" 可以代表0个subsequnce,dp[i][j] = dp[i][j-1]
2. p.charAt(j-1) == '*' 时,若'*'代表的1 or more subsequnce, 则dp[i][j] = dp[i-1][j] 即S: "abcd" 和 P:"ab * " 是否match, 返回去看 S: "abc" 和 P:"ab * " 是否match
代码:
class Solution {
public boolean isMatch(String s, String p) {
boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
dp[0][0] = true;
for(int j = 1; j <= p.length(); j++){
if(p.charAt(j-1) == '*'){
dp[0][j] = dp[0][j-1];
}
} for(int i = 1; i <= s.length() ; i++){
for(int j = 1; j <= p.length(); j++){
if(p.charAt(j-1) == s.charAt(i-1) || p.charAt(j-1) == '?' ){
dp[i][j] = dp[i-1][j-1];
}else{
if (p.charAt(j-1) == '*'){
dp[i][j] = dp[i-1][j] || dp[i][j-1];
}
}
}
}
return dp[s.length()][p.length()];
}
}
[leetcode]44. Wildcard Matching万能符匹配的更多相关文章
- leetcode 44. Wildcard Matching(模糊匹配)
搬运工了- - https://blog.csdn.net/jmspan/article/details/51460021
- LeetCode - 44. Wildcard Matching
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- 第八周 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(字符串匹配问题)
题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single chara ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- 44. Wildcard Matching
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 【leetcode】Wildcard Matching
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
随机推荐
- Written a lua threadpool
工作原理 由于lua只能单线程运行,该lib要求所有lua代码在单线程,而多线程部分只能为c代码 具体用法上要求多线程部分必须用c实现 相关模块 线程池 异步函数实现框架 Now only a sle ...
- NotePad++ 配置Python工作环境
下载地址:https://notepad-plus-plus.org/ Current Version: 7.5.3 sss 显示空格和指标符 为什么建议这么作?因为判断Python语句是否在同一层次 ...
- Python 面向对象(三)
继承的实现原理 Python支持多继承 多继承的时候 属性查找的顺序 研究经典类和新式类在属性查找的不同 主要是形成菱形关系才有深度跟广度 广度优先 Python的继承原理 Python3的内置方 ...
- TypeScript 模块系统
https://www.cnblogs.com/niklai/p/5808789.html
- matplotlib初识
Matplotlib 能够创建多数类型的图表,如条形图,散点图,条形图,饼图,堆叠图,3D 图和地图图表. import matplotlib.pyplot as plt plt.plot([,,], ...
- Python的set集合
set集合也用{}表示,set中的元素是不重复的.无序的,且它里面的元素必须是可hash的(int,str,tuple,bool),set是可变的. 1.使用set去重 m = [1, '] s = ...
- dubbo 支持的7种协议
建议看原文 转自:https://blog.csdn.net/xiaojin21cen/article/details/79834222 1.dubbo 协议 (默认) 2.rmi 协议 3.hes ...
- 批量移动AD用户到指定OU
原文链接:http://blog.51cto.com/shubao/1346469 作为域管理员,在日常工作中使用ADUC(AD用户和计算机)工具在图形界面中进行账号管理操作可谓是家常便饭了.然而一个 ...
- IntelliJ IDEA 中创建maven项目
IDEA作为最好得开发工具之一集成了maven工具,今天记录一下我创建使用idea创建maven项目 1.双击IDEA图标,进入到如下界面,在该页面中,点击箭头所示的“Create New Proje ...
- Centos创建用户
1.创建用户: adduser fish 2.用户设置密码: passwd linuxidc 3.创建文件夹: mkdir fish 4.删除文件夹 rm -rf fish 5.文件夹重命名: mv ...