Wildcard Matching

Implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be:
bool isMatch(const char *s, const char *p) Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false

解法一:

这题想法参考了https://oj.leetcode.com/discuss/10133/linear-runtime-and-constant-space-solution

类似于一种有限状态机的做法。

主要思想如下:

由于*匹配多少个字符是不一定的,于是首先假设*不匹配任何字符。

当后续匹配过程中出错,采用回溯的方法,假设*匹配0个字符、1个字符、2个字符……i个字符,然后继续匹配。

因此s需要有一个spos指针,记录当p中的*匹配i个字符后,s的重新开始位置。

p也需要一个starpos指针指向*的位置,当回溯过后重新开始时,从starpos的下一位开始。

class Solution {
public:
bool isMatch(const char *s, const char *p) {
char* starpos = NULL;
char* spos = NULL; while(true)
{
if(*s == && *p == )
//match all
return true;
else if(*s == && *p != )
{//successive *p must be all '*'
while(*p != )
{
if(*p != '*')
return false;
p ++;
}
return true;
}
else if(*s != && *p == )
{
if(starpos != NULL)
{//maybe '*' matches too few chars in s
p = starpos + ;
s = spos + ;
spos ++; //let '*' matches one more chars in s
}
else
//*s is too long
return false;
}
else
{
if(*p == '?' || *s == *p)
{
s ++;
p ++;
}
else if(*p == '*')
{
starpos = (char*)p;
spos = (char*)s;
p ++;
//start successive matching from "'*' matches non"
}
//currently not match
else if(starpos != NULL)
{//maybe '*' matches too few chars in s
p = starpos + ;
s = spos + ;
spos ++; //let '*' matches one more chars in s
}
else
//not match
return false;
}
}
}
};

解法二:

模仿Regular Expression Matching的递归做法,小数据集上能过。

当*数目太多时会超时。

class Solution {
public:
bool isMatch(const char *s, const char *p) {
if(*p == )
return (*s == );
if(*p == '*')
{
//case1: *p matches 0 chars in s
if(isMatch(s, p+))
return true; //case2: try all possible numbers
while(*s != )
{
s ++;
if(isMatch(s, p+))
return true;
}
return false;
}
else
{
if((*s==*p) || (*p=='?'))
return isMatch(s+, p+);
else
return false;
}
}
};

以下是我的测试代码,小数据上全部通过:

int main()
{
Solution s;
cout << s.isMatch("aa","a") << endl;
cout << s.isMatch("aa","aa") << endl;
cout << s.isMatch("aaa","aa") << endl;
cout << s.isMatch("aa","*") << endl;
cout << s.isMatch("aa","a*") << endl;
cout << s.isMatch("ab","?*") << endl;
cout << s.isMatch("aab","c*a*b") << endl;
return ;
}

最新文章

  1. 游戏机制(Machinations)在线演示工具
  2. Java基础之常用类
  3. Matrix的一些知识
  4. getting started with building a ROS simulation platform for Deep Reinforcement Learning
  5. JQ判断按钮,复选框是否选中
  6. AppStore上架规则
  7. (转)ZOJ 3687 The Review Plan I(禁为排列)
  8. Postman----设置代理抓取手机上的请求
  9. 理解Device Tree Usage(续)
  10. VBS 备份文件
  11. 【转载】FPGA算法设计随笔
  12. HttpClient 通过代理访问验证服务器
  13. Kafka0.10.0安装配置
  14. “全栈2019”Java多线程第二十九章:可重入锁与不可重入锁详解
  15. java环境配置针对win10(电脑重装必备) 最后一步很重要
  16. 起步 - vue-router路由与页面间导航
  17. 20145226夏艺华 《Java程序设计》 课堂实践
  18. BASE64Encoder及BASE64Decoder编译器找不到问题
  19. 关于app更新安装闪退和EditText长按出现的水滴颜色设置问题
  20. Spring中的c3p0配置

热门文章

  1. “==”和equals之间的区别
  2. [开发工具]_[VS2010]_[vs2010的一个bug-使用stringstream时出现]
  3. Python的知识点 plt.plot()函数细节
  4. Pytorch torch.optim优化器个性化使用
  5. python - 增强的格式化字符串format函数
  6. vim学习笔记(2)——vim配置
  7. URAL 1698
  8. Boosted Tree
  9. 当requestFocus不能立刻起作用时…
  10. Linux操作系统安装与VMTools的安装