44. Wildcard Matching (String; DP, Back-Track)
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).不同于正则表达式中的*
*正则表达式的定义:
- '.' Matches any single character.
- '*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial). The function prototype should be:
bool isMatch(const char *s, const char *p) Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
思路I:当遇到*,有把*跳过,和继续保留*两种option=>带回溯的递归。其实也可称之为贪心法,贪心法则是每次都使*匹配尽可能少的字符。
class Solution {
public:
bool isMatch(string s, string p) {
return backTracking(s,p,,);
}
bool backTracking(string s, string p, int sp, int pp){
//end condition
if(sp==s.length()){
while(pp<p.length() &&p[pp]=='*' ){
pp++;
}
if(pp == p.length()) return true;
else return false;
}
if(pp==p.length()) return false;
if(p[pp]=='*'){
while(pp+<p.length() && p[pp+]=='*') pp++; //ignore the stars directly behind star
if(backTracking(s,p,sp,pp+)) return true; //* not repeats
return backTracking(s,p,sp+,pp); //* repeats
}
else if(s[sp]==p[pp] || p[pp]=='?') return backTracking(s,p,sp+,pp+);
else return false;
}
};
时间复杂度:二叉recursion的高度是2n 所以O(2n)
Result: Time Limit Exceeded
思路II:依然是带回溯的递归,只是记录下*号位置,和匹配的字符数,那么等到某次*不匹配时可直接回到该位置。
class Solution {
public:
bool isMatch(string s, string p) {
star = false;
return recursiveCheck(s,p,,);
}
bool recursiveCheck(const string &s, const string &p, int sIndex, int pIndex){
if(sIndex >= s.length()){
while(p[pIndex] == '*' && pIndex < p.length()) pIndex++; //s has went to end, check if the rest of p are all *
return (pIndex==p.length());
}
if(pIndex >= p.length()){
return checkStar(s,p);
}
switch(p[pIndex]) //p: pattern,在p中才可能出现?, *
{
case '?':
return recursiveCheck(s, p, sIndex+, pIndex+);
break;
case '*': //如果当前为*, 那么可认为之前的字符都匹配上了,并且将p移动到 * 结束后的第一个字符
star = true; //p 每次指向的位置,要么是最开始,要么是 * 结束的第一个位置
starIndex = pIndex;
matchedIndex = sIndex-;
while(p[pIndex] == '*'&& pIndex < p.length()){pIndex++;} //忽略紧接在 *后面的*
if(pIndex==p.length()) return true;//最后一位是*
return recursiveCheck(s,p,sIndex,pIndex); //*匹配0个字符
break;
default:
if(s[sIndex] != p[pIndex]) return checkStar(s, p);
else return recursiveCheck(s, p, sIndex+, pIndex+);
break;
}
}
bool checkStar(const string &s, const string &p){
if(!star) return false;
else {
int pIndex = starIndex+;
int sIndex = ++matchedIndex; //回溯,*d多匹配一个字符
return recursiveCheck(s, p, sIndex, pIndex);
}
}
private:
int starIndex;
int matchedIndex;
bool star;
};
Result: Approved.
思路III:使用dp。dp[i][j]表示从字符串到i位置,模式串到j位置是否匹配。
class Solution {
public:
bool isMatch(string s, string p) {
int sLen = s.length();
int pLen = p.length();
if(sLen == ){
int pp = ;
while(pp<p.length() &&p[pp]=='*' ){
pp++;
}
if(pp == p.length()) return true;
else return false;
}
if(pLen == ) return false;
int len = ;
for(int i = ;i < pLen;i++)
if(p[i] != '*') len++;
if(len > sLen) return false;
bool dp[sLen][pLen];
int i = , j = ;
for(;i<sLen;i++){
for(;j<pLen;j++){
dp[i][j]=false;
}
}
if(p[]=='*'){ //c;*?*
for(i = ;i < sLen; i++ ){
dp[i][] = true;
}
}
//first line can appear one letter which is not star
if (p[]=='?' || s[] == p[]){ //first not-star-letter appears
dp[][] = true;
for(j = ;(j < pLen && p[j]=='*'); j++ ){
dp[][j]=true;
}
}
else if(p[]=='*'){
for(j = ;(j < pLen && p[j-]=='*'); j++ ){
if(p[j]=='?' || s[] == p[j]){ //first not-star-letter appears
dp[][j]=true;
j++;
for(;j<pLen && p[j]=='*'; j++){ //after first not star, there should be all star
dp[][j]=true;
}
break;
}
else if(p[j]=='*'){
dp[][j]=true;
}
}
}
for(i = ; i < sLen; i++){
for(j = ; j < pLen; j++){
if(p[j]=='*'){
dp[i][j] = dp[i-][j] //* repeat 1 time
|| dp[i][j-]; //*repeat 0 times
}
else if(s[i]==p[j] || p[j]=='?'){
dp[i][j] = dp[i-][j-];
}
}
}
return dp[sLen-][pLen-];
}
};
时间复杂度:O(n2)
思路IV: 思路III的初始状态求法太复杂=>Solution:定义一个fake head。dp[0][0]表示两个空字符串的匹配情况,dp[0][0]=true.
class Solution {
public:
bool isMatch(string s, string p) {
int sLen = s.length();
int pLen = p.length();
if(sLen == ){
int pp = ;
while(pp<p.length() &&p[pp]=='*' ){
pp++;
}
if(pp == p.length()) return true;
else return false;
}
if(pLen == ) return false;
vector<vector<bool>> dp(sLen+, vector<bool>(pLen+,));
//initial states
int i = , j = ;
dp[][]=true;
for(j = ;(j <= pLen && p[j-]=='*'); j++ ){
dp[][j]=true;
}
//state transfer
for(i = ; i <= sLen; i++){
for(j = ; j <= pLen; j++){
if(p[j-]=='*'){
dp[i][j] = dp[i-][j] //* repeat 1 time
|| dp[i][j-]; //*repeat 0 times
}
else if(s[i-]==p[j-] || p[j-]=='?'){
dp[i][j] = dp[i-][j-];
}
}
}
return dp[sLen][pLen];
}
};
思路V:节约空间,状态之和i-1有关,所以只要记录上一行状态就可以。可以用一维数组。
class Solution {
public:
bool isMatch(string s, string p) {
int sLen = s.length();
int pLen = p.length();
if(sLen == ){
int pp = ;
while(pp<p.length() &&p[pp]=='*' ){
pp++;
}
if(pp == p.length()) return true;
else return false;
}
if(pLen == ) return false;
vector<bool> lastDP(pLen+, );
vector<bool> currentDP(pLen+, );
vector<bool> tmp;
//initial states
int i = , j = ;
lastDP[]=true;
for(j = ;(j <= pLen && p[j-]=='*'); j++ ){
lastDP[j]=true;
}
//state transfer
for(i = ; i <= sLen; i++){
currentDP[]=false;
for(j = ; j <= pLen; j++){
if(p[j-]=='*'){
currentDP[j] = lastDP[j] //* repeat 1 time
|| currentDP[j-]; //*repeat 0 times
}
else if(s[i-]==p[j-] || p[j-]=='?'){
currentDP[j] = lastDP[j-];
}
else{
currentDP[j] = false;
}
}
tmp = currentDP;
currentDP = lastDP;
lastDP = tmp;
}
return lastDP[pLen];
}
};
44. Wildcard Matching (String; DP, Back-Track)的更多相关文章
- 44. Wildcard Matching
题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...
- 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
44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...
- 【LeetCode】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- [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 有简写的字符串匹配
[抄题]: Given an input string (s) and a pattern (p), implement wildcard pattern matching with support ...
- 【一天一道LeetCode】#44. Wildcard Matching
一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...
- 44. Wildcard Matching *HARD*
'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequen ...
随机推荐
- winform messagebox 统一
vb.net 里面有两种messagebox,一种是vb语言提供的msgbox,另一种是.net framework 提供的messagebox.在此统一使用messagebox. Warning,提 ...
- Linux 环境下 javac 编译错误: 编码UTF8的不可映射字符 (编码UTF8/GBK的不可映射字符)
Linux 系统下一般默认使用UTF-8编码, 使用javac 编辑使用其他编码格式编写的源吗时,会出现 “ 错误: 编码UTF8的不可映射字符 ”. 最近在使用 javac 编译 一个在wind ...
- 对MYSQL慢查询slow query 日志记录内容的疑惑
初始:由于新装服务器出现CPU占用过高,响应不及时的问题排查,因为环境基于最基础的LAMP构架 想到开启 MYSQL slow_query_log 慢查询日志做原因分析: 但是看到日志内容之后有点茫然 ...
- Maven与eclipse整合
版权声明: https://blog.csdn.net/zdp072/article/details/37355993 一. 创建Java项目 第1步:首先导入前面命令行建立的两个maven项目Hel ...
- 【转】Windows消息投递流程:WM_COMMAND消息流程
原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182585 该示例通过研究基本的单文档程序的“文件”--“打开”命令,分析WM_COM ...
- 简单实现高并发处理秒杀思路(redis分布式锁)
利用redis的单线程特性 setnx (SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值. getset 自动将key对应到value并且返 ...
- HBuilder webApp开发 Websql增删改查操作
来源:http://blog.csdn.net/zhuming3834/article/details/51471434 这段时间公司要求我们做原生iOS和安卓的都转做H5开发APP,使用的工具HBu ...
- mysql 查询所有父级名称
SELECT T2.id, T2.name FROM ( SELECT @r AS _id, ,,@stop) as stop, (SELECT @r := p_id FROM goods_class ...
- Java 判断是否包含指定的子串 contains()
Java 手册 contains public boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true. 参数: s - 要搜 ...
- python3调用阿里云语音服务
步骤 1 创建阿里云账号,包括语音服务里的企业实名 为了访问语音服务,您需要有一个阿里云账号.如果没有,可首先按照如下步骤创建阿里云账号: 访问阿里云 官方网站,单击页面上的 免费注册 按钮. 按照屏 ...