Wildcard Matching

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). 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

解法一:

这题想法参考了https://oj.leetcode.com/discuss/10133/linear-runtime-and-constant-space-solution

类似于一种有限状态机的做法。

主要思想如下:

由于*匹配多少个字符是不一定的,于是首先假设*不匹配任何字符。

当后续匹配过程中出错,采用回溯的方法,假设*匹配0个字符、1个字符、2个字符……i个字符,然后继续匹配。

因此s需要有一个spos指针,记录当p中的*匹配i个字符后,s的重新开始位置。

p也需要一个starpos指针指向*的位置,当回溯过后重新开始时,从starpos的下一位开始。

class Solution {
public:
bool isMatch(const char *s, const char *p) {
char* starpos = NULL;
char* spos = NULL; while(true)
{
if(*s == && *p == )
//match all
return true;
else if(*s == && *p != )
{//successive *p must be all '*'
while(*p != )
{
if(*p != '*')
return false;
p ++;
}
return true;
}
else if(*s != && *p == )
{
if(starpos != NULL)
{//maybe '*' matches too few chars in s
p = starpos + ;
s = spos + ;
spos ++; //let '*' matches one more chars in s
}
else
//*s is too long
return false;
}
else
{
if(*p == '?' || *s == *p)
{
s ++;
p ++;
}
else if(*p == '*')
{
starpos = (char*)p;
spos = (char*)s;
p ++;
//start successive matching from "'*' matches non"
}
//currently not match
else if(starpos != NULL)
{//maybe '*' matches too few chars in s
p = starpos + ;
s = spos + ;
spos ++; //let '*' matches one more chars in s
}
else
//not match
return false;
}
}
}
};

解法二:

模仿Regular Expression Matching的递归做法,小数据集上能过。

当*数目太多时会超时。

class Solution {
public:
bool isMatch(const char *s, const char *p) {
if(*p == )
return (*s == );
if(*p == '*')
{
//case1: *p matches 0 chars in s
if(isMatch(s, p+))
return true; //case2: try all possible numbers
while(*s != )
{
s ++;
if(isMatch(s, p+))
return true;
}
return false;
}
else
{
if((*s==*p) || (*p=='?'))
return isMatch(s+, p+);
else
return false;
}
}
};

以下是我的测试代码,小数据上全部通过:

int main()
{
Solution s;
cout << s.isMatch("aa","a") << endl;
cout << s.isMatch("aa","aa") << endl;
cout << s.isMatch("aaa","aa") << endl;
cout << s.isMatch("aa","*") << endl;
cout << s.isMatch("aa","a*") << endl;
cout << s.isMatch("ab","?*") << endl;
cout << s.isMatch("aab","c*a*b") << endl;
return ;
}

【LeetCode】44. Wildcard Matching (2 solutions)的更多相关文章

  1. 【leetcode】44. Wildcard Matching

    题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...

  2. 【一天一道LeetCode】#44. Wildcard Matching

    一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches a ...

  3. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  4. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  5. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  6. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  7. 【LeetCode】28. Implement strStr() (2 solutions)

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

  8. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  9. 【LeetCode】1023. Camelcase Matching 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+字典 日期 题目地址:https://leet ...

随机推荐

  1. 最小二乘法least square

    上研究生的时候接触的第一个Loss function就是least square.最近又研究了一下,做个总结吧. 定义看wiki就够了.公式如下 E(w)=12∑n=1N{y−xWT}2E(w)=12 ...

  2. LXT技术平台(Lenovo Trust Technology)

    LXT技术平台(L是Lenovo的缩写,T是技术Technology,X代表多个应用方向),是联想以用户需求为导向,整合先进技术,为用户提供最佳应用体验的一体化解决方案. 中文:LXT技术平台 外文: ...

  3. poj 1007 Quoit Design(分治)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. iOS:UITableView表格视图控件

    UITableView:表格视图控件,继承滚动视图控件UIScrollView,(类似于UIPickerView选择器,它主要通过设置数据源代理和行为代理实现协议来设置单元格)    对表格的操作主要 ...

  5. Informatica 常用组件Lookup之五 转换属性

    查找转换的属性标识数据库源.PowerCenter 如何处理转换,以及它如何处理高速缓存和多项匹配. 创建映射时,为每个查找转换指定属性.创建会话时,您可在会话属性中覆盖某些属性,如每个转换的索引和数 ...

  6. 关于asp.net页面缓存

    1,ASPX页面缓存 页面缓存的使用方法非常的简单,只需要在aspx页的顶部加一句声明<%@ OutputCache Duration="60" VaryByParam=&q ...

  7. JavaScript游戏中的面向对象的设计

    简介: 从程序角度考虑,许多 JavaScript 都基于循环和大量的 if/else 语句.在本文中,我们可了解一种更聪明的做法 — 在 JavaScript 游戏中使用面向对象来设计.本文将概述原 ...

  8. 【K8S】K8S-网络模型、POD/RC/SVC YAML 语法官方文档

    K8S-网络模型.POD/RC/SVC YAML 语法官方文档 Kubernetes - Production-Grade Container Orchestration kubernetes/kub ...

  9. linux下的tar命令

    tar 命令在打包的时候如果是通过绝对路径压缩打包,在不特殊参数时,解压时会在当前路径下创建打包时的路径,并提示警告: tar: Removing leading `/' from member na ...

  10. 火狐浏览器Firefox Firebug使用方法

    什么是Firebug 从事了数年的Web开发工作,越来越觉得现在对WEB开发有了更高的要求.要写出漂亮的HTML代码:要编写精致的CSS样式表展示每个页面模块:要调试javascript给页面增加一些 ...