The recursive program will result in TLE like this:

class Solution {
public:
bool isMatch(const char *s, const char *p) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (*s == *p && *s == '\0')
return true;
if (*p == '?' || *s == *p)
return isMatch(s + 1, p + 1);
else if (*p == '*') {
int i;
for (i = 0; *(s + i); ++i)
if (isMatch(s + i, p + 1))
return true;
if (isMatch(s + i, p + 1))
return true; return false;
}
else if (*p != *s)
return false;
}
};

So it's necessary to write an non-recursive program. The key point is to match the '*' in p string. We could attempt to match '*' with 0...n characters in s, i.e.,
the character after '*' maybe match any position in s regardless a series of characters in s. Take notice that
consecutive '*'s are equal to one '*'. Based on that, a lengthy code is written as :

class Solution {
public:
bool isMatch(const char *s, const char *p) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
bool star = false, staremerge = false;
const char *str = s, *ptr = p, *ss = s, *pp = p;
for (str = ss, ptr = pp; *str && *ptr || *str == '\0' && *ptr == '*'; ++str, ++ptr) {
if (*ptr == '*') {
star = staremerge = true;
while (*ptr == '*')
++ptr;
if (*ptr == '\0')
return true;
ss = str;
pp = ptr;
--str;
--ptr;
}
else {
if (!star) {
if( !staremerge ) {
if (*str != *ptr && *ptr != '?' ||*(ptr + 1) == '\0' && *(str + 1) != '\0')
return false;
}
else {
if (*str != *ptr && *ptr != '?' ||*(ptr + 1) == '\0' && *(str + 1) != '\0') {
str = ss++;
ptr = pp - 1;
star = true;
} }
}
else if (star) {
if ( *str != *ptr && *ptr != '?') {
ss = str + 1;
--ptr;
}
else {
star = false;
if (*(ptr + 1) == '\0' && *(str + 1) != '\0') {
str = ss++;
ptr = pp - 1;
star = true;
}
}
}
}
}
if (*str == *ptr && *str == '\0')
return true;
else
return false;
}
};

Some suggestions about this code:

1. There is no need to refresh the status of star, staremerge. Only one star is enough, because the character(for example, 'a') always needs to match some 'a' in s. Matching the former 'a' is better than the latter 'a' in s as is illustrated in the figure. I.e., there is no need to record the matching range for every '*', the latest '*' has the largest range of choice.

2. sbegin is refreshed when mismatch occurs and pbegin is refreshed when meeting new '*';

3. Focusing on s is better than handling the two strings at the same time.

So the final concise code is like:

class Solution {
public:
bool isMatch(const char *s, const char *p) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
const char *sbegin = s, *pbegin = p, *str = s, *ptr = p;
bool star = false;
for (str = s, ptr = p; *str || *ptr == '*'; ++str, ++ptr) {
if (*ptr == '*') {
star = true;
while (*ptr == '*')
++ptr;
if (*ptr == '\0')
return true;
pbegin = ptr--;
sbegin = str--;
}
else if (*str != *ptr && *ptr != '?'){
if (!star)
return false;
str = sbegin++;
ptr = pbegin - 1;
}
}
return *ptr == '\0';
}
};

leetcode Wildcard Matching greedy algrithm的更多相关文章

  1. LeetCode: Wildcard Matching 解题报告

    Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...

  2. [LeetCode] Wildcard Matching 题解

    6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...

  3. [LeetCode] Wildcard Matching 外卡匹配

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  4. [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  5. [Leetcode] Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  6. [leetcode]Wildcard Matching @ Python

    原题地址:https://oj.leetcode.com/problems/wildcard-matching/ 题意: Implement wildcard pattern matching wit ...

  7. [Leetcode] Wildcard matching 通配符匹配

    Implement wildcard pattern matching with support for'?'and'*'. '?' Matches any single character. '*' ...

  8. [LeetCode]Wildcard Matching 通配符匹配(贪心)

    一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...

  9. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

随机推荐

  1. GPS坐标定位与距离计算

    Android获取当前位置(GPS和网络定位) 1.比较: GPS准确度高但耗电多,网络定位耗电少但准确度低 2.代码 ①添加权限: AndroidManifest.xml: <!-- 两种pr ...

  2. [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

    相信如果用谷歌浏览器做移动端页面的时候 用touch事件的时候应该遇到过这个东东吧 documet.addEventListener("touchstart",function() ...

  3. HOWTO: Get the command line of a process(转)

    How would you get the command line of a process? Some people have suggested that you use remote thre ...

  4. 【scrapy】使用方法概要(一)(转)

    [请初学者作为参考,不建议高手看这个浪费时间] 工作中经常会有这种需求,需要抓取互联网上的数据.笔者就经常遇到这种需求,一般情况下会临时写个抓取程序,但是每次遇到这种需求的时候,都几乎要重头写,特别是 ...

  5. python文本 去掉字符串前后空格

    python文本 去掉字符串前后空格 场景: 去掉字符串前后空格 可以使用strip,lstrip,rstrip方法 >>> a="abc".center (30 ...

  6. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  7. 使用开源库 MBProgressHUD 等待指示器

    source https://github.com/jdg/MBProgressHUD MBProgressHUD is an iOS drop-in class that displays a tr ...

  8. JTable常见用法细则

    JTable是Swing编程中很常用的控件,这里总结了一些常用方法以备查阅.欢迎补充,转载请注明作者与出处. 一.创建表格控件的各种方式:1)  调用无参构造函数. JTable table = ne ...

  9. tomcat完整配置

    规划: 网站网页目录:/web/www 域名:www.test1.com 论坛网页目录:/web/bbs URL:bbs.test1.com/bbs 网站管理程序:$CATALINA_HOME/wab ...

  10. Spark源码分析

    名词解释 RDD全称为ResilientDistributedDataset,弹性分布式数据集.就是分布在集群节点上的数据集,这些集合可以用来进行各种操作.最重要的一点是,某个操作计算后的数据集可以缓 ...