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. BZOJ3473 字符串 广义后缀自动机

    今天主攻了下SAM 好多东西以前都没理解到 对于这道题 我们建一个自动机存所有串 每个穿last从1开始 对于自动机上每个点额外记一个cnt 表示能匹配到这个点的不同串个数 建完对每个串在自动机上匹配 ...

  2. Disable File System Redirector For Windows x64 (Python recipe)(转)

    This disables the Windows File System Redirector.When a 32 bit program runs on a 64 bit operating sy ...

  3. sqlite - Sqlite Wrappers - Delphi

    http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers Aducom's SQLite: Open source (NewBSD) Delphi (4. ...

  4. 电感式DC/DC变换器工作原理

    http://www.amobbs.com/thread-3293203-1-1.html 首先必须要了解电感的一些特性:电磁转换与磁储能.其它所有参数都是由这两个特性引出来的. 电感回路通电瞬间 断 ...

  5. C语言每日小练(四)——勇者斗恶龙

    勇者斗恶龙 你的王国里有一条n个头的恶龙,你希望雇佣一些骑士把它杀死(砍掉全部的头). 村里有m个骑士能够雇佣.一个能力值为x的骑士能够砍掉恶龙一个致敬不超过x的头,且须要支付x个金币. 怎样雇佣骑士 ...

  6. symbol(s) not found for architecture armv7

    Undefined symbols for architecture i386: “_OBJC_CLASS_$_XXX”, referenced from: objc-class-ref in XXX ...

  7. LR杂记-nmon+analyser监控linux系统资源

    1.查看linux具体版本号信息 file /sbin/init 2.下载相应nmon版本号 http://pkgs.repoforge.org/nmon/ 3.安装 rpm -ivh nmon-14 ...

  8. SSL协议具体解释

    背景介绍    近期在看<password学与网络安全>相关的书籍,这篇文章主要具体介绍一下著名的网络安全协议SSL. 在開始SSl介绍之前,先给大家介绍几个password学的概念和相关 ...

  9. Peter Norvig:学习在于挑战和重复

    黄小非译注(本文来自伯乐在线):本文作者Peter Norvig目前任职于Google,其职位是研究主管(Director of  Research). Peter Norvig是享誉世界的计算机科学 ...

  10. Linux下分割、合并PDF(pdftk),用于Linux系统的6款最佳PDF页面裁剪工具

    Linux下分割.合并PDF(pdftk),用于Linux系统的6款最佳PDF页面裁剪工具 Linux下分割.合并PDF(pdftk) pdftk http://www.pdflabs.com/doc ...