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. HTML的各个标签的默认样式

    head{ display: none } body{ margin: 8px;line-height: 1.12 } button, textarea,input, object,select { ...

  2. Go语言Web框架gwk介绍 (五)

    Session Go的net/http本身不带session的机制,需要开发人员自行实现,gwk实现了内存中的session存储机制,如果需要将session存在其他地方比如redis或者memcac ...

  3. 力特ZE398C驱动光盘-USB转RS232-支持Windows 10/Mac

    这个工具是USB1.1的,相对来说比较老,一开始做小白鼠不知道买了USB1.1的,所以我不建议买这个,还有其它的型号,支持USB2.0和USB3.0,不过价格也相对来说比较贵,这个才30块钱左右. 关 ...

  4. DotNet_Performance_Tuning_ANTS_Performance_Profiler

    http://www.cnblogs.com/parry/archive/2013/01/04/DotNet_Performance_Tuning_ANTS_Performance_Profiler. ...

  5. patch补丁命令 P1 P0 P2

    http://fancyxinyu.blog.163.com/blog/static/1823213662013719115245699/ http://blog.chinaunix.net/uid- ...

  6. axure8.1可用授权码

    Licensee: University of Science and Technology of China (CLASSROOM)Key: DTXRAnPn1P65Rt0xB4eTQ+4bF5IU ...

  7. .NET泛型03,泛型类型的转换,协变和逆变

    协变(Convariant)和逆变(Contravariant)的出现,使数组.委托.泛型类型的隐式转换变得可能. 子类转换成基类,称之为协变:基类转换成子类,称之为逆变..NET4.0以来,支持了泛 ...

  8. 重写Html.DropDownList和Html.DropDownListFor的name属性

    □ 重写前 通常这样写: @Html.DropDownListFor(m => m.DelFlag,(List<SelectListItem>)ViewBag.d,"==请 ...

  9. A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28

    上面回答有问题,找到qq官方的文档了 http://service.exmail.qq.com/cgi-bin/help?id=28&no=1000585&subtype=1 如果您的 ...

  10. 让子弹飞Demo版

    让子弹飞是我非常喜欢的一款游戏.今天的目标就是利用cocos2dx 3.0 和box2d 打造一款这样的类型游戏的Demo版.本来cocos2dx 3.0 已经封装了physicals模块,可是我在使 ...