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 ...
随机推荐
- Linux:关于设置PS1提示符输入长命令格式出现的问题及解决
关于设置PS1提示符命令输出格式出现的问题解决 正确的格式 \[\e[;;32m\]xxxx #如果只是改变提示符而不改变命令,在后面加一个结束符. \[\e[;;32m\]xxxx \[\e[0m ...
- asp.net 操作word 权限
1.先安装office 2.在“DCOM配置”中,为IIS账号配置操作Word(其他Office对象也一样)的权限: 开始>运行>输入 dcomcnfg >确定 具体操作:“组件 ...
- java编程之常见的排序算法
java常见的排序算法 第一种:插入排序 直接插入排序 1, 直接插入排序 (1)基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排 好顺序的,现在要把第n个数插到前面的 ...
- CH1812 生日礼物
题意 描述 ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, ..., AN. 她被允许选择不超过 M 个连续的部分作为自己的生日礼物.ftiasch想要知道选 ...
- baby用品
新生嬰兒用品清單 1.哺育用品: 大奶瓶:6支,240ml左右.選擇PC材質耐高溫120度,可消毒:玻璃材質建議選用印刷安全無鉛材料,可消毒. 小奶瓶:2-3支,120ml左右.寬口徑/一般口徑(喝水 ...
- java局部变量和临时变量
局部变量:temp=1, 临时变量:return a+b 临时变量会有一点的性能优势 局部变量会比成员变量和静态成员变量有优势,改进的方法是吧成员变量和静态成员变量赋值在局部变量:https://bl ...
- 用arm-linux-gnueabihf移植MP3播放器libmad-0.15.1b的时候出现错误提示
diff --git a/package/libmad/libmad-0.15.1b-thumb2-fixed-arm.patch b/package/libmad/libmad-0.15.1b-th ...
- Httpclient 支持https(转)
参考:https://jingyan.baidu.com/article/154b46317353d228ca8f4112.html 参考:https://www.jianshu.com/p/a444 ...
- 【linux】head&&tail
命令: head[-n][文件名] tail[-n][-f][文件名] 形式: -n 行数 (显示前几行) ...
- 黄聪:VS2010中如何让webbrowser不弹出JS异常错误窗口(c#.net)
1.在属性窗口找到ScriptErrorsSuppressed,选择"true",这个选择的意思是,如果网页上有出现错误命令,这个错误提示将被抑制 2.[项目管理那里,在项目上右击 ...