leetcode Wildcard Matching greedy algrithm
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的更多相关文章
- LeetCode: Wildcard Matching 解题报告
Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...
- [LeetCode] Wildcard Matching 题解
6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [Leetcode] Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [leetcode]Wildcard Matching @ Python
原题地址:https://oj.leetcode.com/problems/wildcard-matching/ 题意: Implement wildcard pattern matching wit ...
- [Leetcode] Wildcard matching 通配符匹配
Implement wildcard pattern matching with support for'?'and'*'. '?' Matches any single character. '*' ...
- [LeetCode]Wildcard Matching 通配符匹配(贪心)
一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
随机推荐
- ZOJ 3626 Treasure Hunt I 树上DP
E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...
- webpack vuejs 和 vue-router 如何使用?
读本文之前,建议对webpack和vuejs有初步的了解,通过webpack的官网和vuejs的中文官网了解即可 网站主要目录://某些文件不一定全部罗列出来,注意观察 vue-wepack -src ...
- WINDOWS 的 MKLINK : 硬链接,符号链接 : 文件符号链接, 目录符号链接 : 目录联接
玩转WIN7的MKLINK 引言: 换了新电脑,终于再次使用上啦WIN7 ,经过一个周每天重装N次系统,... ... ... ... 在xp系统下,junction命令要用微软开发的小程序 junc ...
- Tasker App Factory
http://tasker.dinglisch.net/userguide/en/appcreation.html App Creation Introduction Hello World Exam ...
- windows的磁盘操作之九——区分本地磁盘与移动硬盘
http://cutebunny.blog.51cto.com/301216/674443 最近碰到了个新问题,记录下来作为windows的磁盘操作那个系列的续篇吧. 一些时候我们的程序需要区分本地存 ...
- 最快的BT软件rtorrent Step by Step指南
原文地址:http://forum.ubuntu.org.cn/viewtopic.php?t=165069 rtorrent是linux下最快的bt下载软件,由于支持DHT网络,可以很好的于迅雷和B ...
- chromium对网页获取favicon
每一个网页都有一个favicon,在历史记录的保存中须要用到.在content文件夹下,这个没有实现. 以下说一下我的实现过程: web_contents_impl.cc文件里有方法:WebConte ...
- Jetty学习二:配置概览-怎么配置Jetty
Jetty POJO配置 Jetty的核心组件是Plain Old Java Objects(POJOs):配置Jetty的大部分工作就是在Jetty POJOs上的初始化.装配和设置域的处理,你能通 ...
- 《趣学Python编程》
<趣学Python编程> 基本信息 作者: (美)Jason Briggs 译者: 尹哲 出版社:人民邮电出版社 ISBN:9787115335951 上架时间:2014-2-21 出版日 ...
- mysqld_safe脚本执行的基本流程
mysqld_safe脚本执行的基本流程:1.查找basedir和ledir.2.查找datadir和my.cnf.3.对my.cnf做一些检查,具体检查哪些选项请看附件中的注释.4.解析my.cnf ...