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 ...
随机推荐
- MySql查询生日的两种方式
需要是要查询日期段内过生日的会员,分为两种情况: 1. 不跨年 例如: 查询2017-01-01到2017-01-20之间过生日的会员 (假定今天是2017-01-01则这种也可以描述为20天内过生 ...
- spring boot 教程(三)配置详解
在大部分情况下,我们不需要做太多的配置就能够让spring boot正常运行.在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性. Spring Boot 支持多种外部配置方式 这些 ...
- [QT][DEMO] QTableWidget 设置某一列禁止编辑
例程 : 又是好风景 : http://blog.csdn.net/qiao_yihan/article/details/46413345 关键点: 1.QTableWidgetItem 的 setF ...
- Spring的JDBC Template
Spring的JDBC Template(JDBC模板)简化JDBC API开发,使用上和Apache公司的DBUtils框架非常类似) 快速入门实例 1.创建项目后,导入Spring基础核心开发包. ...
- .NET CORE微服务实践
.NET CORE微服务实践 https://www.cnblogs.com/zengqinglei/p/9570343.html .NET CORE 实践部署架构图 实践源码:https://git ...
- 《FDTD electromagnetic field using MATLAB》读书笔记之 Figure 1.14
背景: 基于公式1.42(Ez分量).1.43(Hy分量)的1D FDTD实现. 计算电场和磁场分量,该分量由z方向的电流片Jz产生,Jz位于两个理想导体极板中间,两个极板平行且向y和z方向无限延伸. ...
- php str_replace替换特殊字符
替换单引号,双印,正斜杠,反斜杠等等,and,select等等 <?php $str = 'a\bc"1\'2d4/e/fgh\\\abc\\//a"bandan\'and' ...
- javaweb经典面试题
1.hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得java程序员可以随心所欲的使用对象编程思维来操纵数据库. 工作原理: 1.读取并解析配置文件2. ...
- CentOS 6安装php加速软件Zend Guard(转)
(尚未验证) PHP5.3以上的版本不再支持Zend Optimizer,已经被全新的 Zend Guard Loader 取代,下面是安装Zend Guard具体步骤,以下操作均在终端命令行执行 1 ...
- python 书籍推荐 三
主要先学习<python语言入门>学完后,研究<征服python>Python简明教程(A Byte of Python) 此书讲解简洁易懂,适合初学者 剖析Python源代码 ...