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. Elasticsearch基础分布式架构

    写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...

  2. HDU 5700 区间交 离线线段树

    区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...

  3. CentOS 7 下编译安装lnmp之nginx篇详解

    一.安装环境 宿主机=> win7,虚拟机 centos => 系统版本:CentOS Linux release 7.5.1804 (Core),ip地址 192.168.1.168   ...

  4. JS数组与PHP数组的对比

    一.分类与创建 1:JS的数组都是索引数组,数组是一种特殊的对象,创建数组的方式有两种 //方式一 var arr1 = ['关羽', '张飞', '赵云', '马超', '黄忠']; //方式二 v ...

  5. 极路由通过SSH添加静态路由表之后无法跳转的问题

    1.确定系统已经开启了转发功能: /etc/sysctl.conf下的配置项目为net.ipv4.ip_forward = 1 2.关闭防火墙的REJECT,也就是修改/etc/config/fire ...

  6. HDU 4716 A Computer Graphics Problem (水题)

    A Computer Graphics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  7. Mybatis最入门---代码自动生成(generatorConfig.xml配置)

    [一步是咫尺,一步即天涯] 经过前文的叙述,各位看官是不是已经被Mybatis的强大功能给折服了呢?本文我们将介绍一个能够极大提升我们开发效率的插件:即代码自动生成.这里的代码自动生成包括,与数据库一 ...

  8. [Node.js]Path模块

    摘要 path模块提供了一些处理文件路径问题的工具. path模块 引入模块 var path=require("path"); 方法 1 path.normalize(p)规范化 ...

  9. MVC自定义编辑视图,DateTime类型属性显示jQuery ui的datapicker

    实现的效果为:在编辑视图中,对DateTime类型的属性,显示jQuery UI的datepicker.效果如下: Student.cs public class Student    {       ...

  10. Nginx HTTP负载均衡/反向代理的相关参数测试

    原文地址:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/03/15/1984976.html 测试目的 (1)弄清楚HTTP Upstr ...