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 == 0 && *p == 0)//match allreturn true;else if(*s == 0 && *p != 0){//successive *p must be all '*'while(*p != 0){if(*p != '*')return false;p ++;}return true;}else if(*s != 0 && *p == 0){if(starpos != NULL){//maybe '*' matches too few chars in sp = starpos + 1;s = spos + 1;spos ++;    //let '*' matches one more chars in s
                }else//*s is too longreturn 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 matchelse if(starpos != NULL){//maybe '*' matches too few chars in sp = starpos + 1;s = spos + 1;spos ++;    //let '*' matches one more chars in s
                }else//not matchreturn false;}}}
};

解法二:

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

当*数目太多时会超时。

class Solution {
public:bool isMatch(const char *s, const char *p) {if(*p == 0)return (*s == 0);if(*p == '*'){//case1: *p matches 0 chars in sif(isMatch(s, p+1))return true;//case2: try all possible numberswhile(*s != 0){s ++;if(isMatch(s, p+1))return true;}return false;}else{if((*s==*p) || (*p=='?'))return isMatch(s+1, p+1);elsereturn 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 0;
}

转载于:https://www.cnblogs.com/ganganloveu/p/4161767.html

【LeetCode】44. Wildcard Matching (2 solutions)相关推荐

  1. 【LeetCode】69. Sqrt(x) (2 solutions)

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)= ...

  2. 【LeetCode】48. Rotate Image (2 solutions)

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  3. 【leetcode】1023. Camelcase Matching

    题目如下: A query word matches a given pattern if we can insert lowercaseletters to the pattern word so ...

  4. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

  5. 【To Understand! 重点 递归 动态规划 正则表达式匹配】LeetCode 44. Wildcard Matching

    LeetCode 44. Wildcard Matching Solution1:我的答案 递归,时间复杂度是O(2n)O(2n)O(2^n)因为超时未能AC 只是记录一下,以警示后人-- class ...

  6. 【LeetCode】剑指 Offer 44. 数字序列中某一位的数字

    [LeetCode]剑指 Offer 44. 数字序列中某一位的数字 文章目录 [LeetCode]剑指 Offer 44. 数字序列中某一位的数字 package offer;public clas ...

  7. LeetCode: 44. Wildcard Matching

    LeetCode: 44. Wildcard Matching 题目描述 Implement wildcard pattern matching with support for '?' and '* ...

  8. LeetCode - 44. Wildcard Matching

    44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...

  9. LeetCode 10. Regular Expression Matching / 44. Wildcard Matching

    10. Regular Expression Matching 经典DP题目,比较复杂,需要多复习. dp[i][j] 表示 s 下标0~i,p 下标0~j 是否能够匹配   dp[i-1][j-1] ...

最新文章

  1. 单元测试案例(白盒测试)
  2. 一起谈.NET技术,在.NET Workflow 3.5中使用多线程提高工作流性能
  3. 数学之路-分布式计算-disco(4)
  4. Ubuntu软件安装
  5. 《Python游戏趣味编程》 第4章 疯狂的小圆圈
  6. iphone NSTimer
  7. 凸优化系列一:什么是最优化算法
  8. Unity实现鼠标点击指定位置导航角色
  9. 大西瓜支付宝/QQ/微信收款码三合一源码+实测可用
  10. vue项目中canvas两张图片生成合并成一张图片 canvas二维码放图片上
  11. github android涂鸦,Android 涂鸦最佳实践
  12. 「BIND9」- DLZ(Dynamically Loadable Zones) @20210212
  13. Python 函数 sort(),sorted() 之区别及 key=lambda x:x[] 之理解
  14. 30行Python代码实现自动收发邮件
  15. 计算机专业当兵退役什么工作,当兵退伍后能做什么 能干什么工作
  16. AP_HAL 分析, 以pixhawk-fmuv2为硬件平台,ChibiOS为底层操作系统:
  17. 微信公众号文章已经获授权加入白名单了,为什么系统还是自动替换成原文章?
  18. python 储存汉字_汉字存储字节
  19. 最新!国有高速大数据最新解读及场景浅析
  20. IT项目量化管理:细化、量化与图形化 与 中国IT项目实施困惑

热门文章

  1. Gym - 100543L
  2. 记一次webpack4+react+antd项目优化打包文件体积的过程
  3. Makefile的介绍与使用
  4. 大数据入门第二十天——scala入门(二)scala基础02
  5. C语言实现排名算法和排位算法
  6. 怎么使用java官方demo?
  7. hihocoder offer收割编程练习赛8 B 拆字游戏
  8. POJ1155 TELE(树形DP)
  9. core java 8~9(GUI AWT事件处理机制)
  10. 【好用的ORM框架】