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 ...
随机推荐
- SSRS 在使用矩阵的时候,隐藏掉一列空白值
SSRS 在使用矩阵的时候会因为数据集中含有NULL导致出现一列空白值 数据结果如图: 然后以houseid 作为矩阵组列,productcode作为行, 列名196前面多出就是NUll的列,那么我们 ...
- mysql数据添加时如果这条数据存在进行修改
1.建表 CREATE TABLE vipMovie( id INT PRIMARY KEY AUTO_INCREMENT, md_name VARCHAR(255) NOT NULL UNIQUE, ...
- MySQL 服务正在启动 .MySQL 服务无法启动。系统出错。发生系统错误 1067。进程意外终止。
MySQL 服务正在启动 .MySQL 服务无法启动.系统出错.发生系统错误 1067.进程意外终止. 检查了一个晚上才发现是---配置问题 #Path to installation directo ...
- 从JDK源码角度看Short
概况 Java的Short类主要的作用就是对基本类型short进行封装,提供了一些处理short类型的方法,比如short到String类型的转换方法或String类型到short类型的转换方法,当然 ...
- goldendict
linux下的翻译词典,可以添加在线和离线词典,比window下的有道感觉强的不止100倍. 点击编辑—>dictionary,可以添加在线和离线词典,最好添加离线的把,我添加了好多在线的,go ...
- streamsets Executors 说明
执行程序阶段在收到事件时触发任务.执行者不会写入或存储事件. 将执行程序用作事件流中数据流触发器的一部分,以执行事件驱动的与管道相关的任务,例如在目标关闭时移动完全写入的文件. 可以使用的execut ...
- [C++/Python] 如何在Python中使用一个DLL? (Windows环境)
开发环境VS2012, WIN7 64. 首先生成的DLL大致如下: .h文件 #ifdef CVINPYTHON_EXPORTS #define CVINPYTHON_API __declspec( ...
- 【转】UBUNTU 下GIT的安装
原文网址:http://www.cnblogs.com/perseus/archive/2012/01/06/2314069.html linux下软件的安装方式有多种,最简单的莫过于从软件中心直接安 ...
- WIN10下搭建react-native开发Android环境
最近公司要求使用react-native进行移动端开发,据说macOS上开发坑会少的多,但我们是windows,莫法,直接抗吧!周末配置环境遇到很多问题,谨以此文做个记录... 准备 安装Chocol ...
- EasyUI使用小常识
datagrid:1 //显示某列 $('#ListTable').datagrid('showColumn', 'ExRate'); //隐藏某列 $('#ListTable').datagrid( ...