[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:
scould be empty and contains only lowercase lettersa-z.pcould 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 ...
随机推荐
- Vue创建头部组件示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...
- Java_IO异常处理方式_入门小笔记
package IO; import java.io.FileWriter; import java.io.IOException; /** * IO异常处理方式 */ class FileWrite ...
- Redis缓存系统(一)Java-Jedis操作Redis,基本操作以及 实现对象保存
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/jiangtao_st/article/details/37699473 源码下载: http://d ...
- 数字证书的理解以及自建CA机构颁发证书
一.理解什么是数字证书 http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html 理解数字证书等概念,无数次想好好看 ...
- win7中安装mysql
这篇文章主要介绍了如何在win7中安装mysql,所以加上了MySQL的下载过程,希望对需要的人有所帮助大家都知道MySQL是一款中.小型关系型数据库管理系统,很具有实用性,对于我们学习很多技术都有帮 ...
- ios-微信支付登录分享-notification通知
// // AppDelegate.m // NewAppBase // // Created by ENERGY on 2018/5/17. // Copyright © 2018年 ENE ...
- 【maven】之nexus常用的一些配置
nexus私服主要是在项目和maven中央仓库中间做代理,一般在公司内网或者公司内部的一些私包,都需要这么个产品.下面主要是关于maven和nexus之间的一些配置 1.在pom中配置nexus私服 ...
- mysql识别中文
在配置的INI中加上这些 [mysql]default-character-set=utf8no-auto-rehash# Remove the next comment character if y ...
- Java中final关键字修饰变量、方法、类的含义是什么
Java中的关键字final修饰变量.方法.类分别表示什么含义? 先看一个简单的介绍 修饰对象 解释说明 备注 类 无子类,不可以被继承,更不可能被重写. final类中的方法默认是final的 方法 ...
- [UE4]VR角色形象:Lock to Hmd、Use Pawn Control Rotation
Camera组件是自动跟着头显一起移动的,所以只要给Camera的子控件添加一个Static Mesh或者Skeletal Mesh并选择合适的模型就可以了. 要记得勾选Lock to Hmd(锁定到 ...