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

思路:我们维护两个指针inds和indp,分别代表s和p字符串中当前要比较的位置。

如果s[inds] 等于 p[indp],或者p[indp]为'?',则匹配成功,inds和indp分别加一,进行下一次匹配。

如果p[indp]为'*',则记下当前两个字符串匹配的下标,即s_star = inds,p_star = indp。

当我们遇见*号时,*号可以与0到多个字符进行匹配,因此这里我们从匹配0个开始尝试。即令indp加1,然后开始下一轮的匹配(inds未变,而indp加1了,相当于p字符串里这个*号与s中的0个字符进行了匹配)。

当我们发现上述情况均不成立,即s[inds]和p[indp]匹配失败时,看一下p_star的值是否大于-1(初始值为-1,大于-1说明前面遇见了*号),若大于-1,说明之前有*号,然后我们尝试*号再多匹配一个字符,即我们令inds=++s_star,然后indp=p_star + 1,进行下一次迭代。

若上述条件都不成立,那肯定是匹配不上了,return false。

在实现过程中,我们通过inds < s.size()这个条件来判断迭代是否要结束。而在迭代过程中,我们要时刻保证indp < p.size()。如果在迭代中出现了indp = p.size(),说明s和p是不匹配的。

在迭代结束后,我们不能马上就下定论,因为有可能p串的末尾有多个*号我们没有进行完,而*号可以匹配0个符号,所以我们要考虑到这种情况。这里我们进行一下小处理,只要indp < p.size()且p[indp]='*',就令indp加一。

最后判断indp是否到了p串的末尾就知道是否匹配了。

 class Solution {
public:
bool isMatch(string s, string p) {
int slen = s.size(), plen = p.size();
int inds = , indp = ;
int s_star = -, p_star = -;
while (inds < slen)
{
if (indp < plen && p[indp] == '*')
{
s_star = inds;
p_star = indp++;
}
else if (indp < plen && (s[inds] == p[indp] || p[indp] == '?'))
{
inds++;
indp++;
}
else if (p_star > -)
{
inds = ++s_star;
indp = p_star + ;
}
else return false;
}
while (indp < plen && p[indp] == '*')
indp++;
return indp == plen;
}

Wildcard Matching - LeetCode的更多相关文章

  1. Wildcard Matching leetcode java

    题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...

  2. [Leetcode][Python]44:Wildcard Matching

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...

  3. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  4. LeetCode: Wildcard Matching 解题报告

    Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...

  5. [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching

    开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...

  6. 【leetcode】Wildcard Matching

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  7. LeetCode - 44. Wildcard Matching

    44. Wildcard Matching Problem's Link --------------------------------------------------------------- ...

  8. [LeetCode] Wildcard Matching 题解

    6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...

  9. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

随机推荐

  1. 使用Visual Studio建立报表--C#

    原文:使用Visual Studio建立报表--C# 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_23893313/article/deta ...

  2. POJ 2771 Guardian of Decency (二分图最大点独立集)

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 25 ...

  3. aircrack-ng破解wlan无线流量包

    记录一下新的知识点. 无线协议里最关键的就是EAPOL协议了,这个里面保存着密钥,所以破解无线流量包也应该从这里入手. 用到的工具是aircrack-ng,这个在kali自带,也可以下载windows ...

  4. lcd1602如何自定义汉字(verilog)

    今天一鼓作气,再研究了一下如何用LCD1602自定义汉字 1.用字模软件获取汉字所对应的数据(因为嫌麻烦所以直接用了网上一个帖子里有关“电”的数据,如下:04,1f,15,1f,15,15,1f,04 ...

  5. csapp 深入理解计算机系统 csapp.h csapp.c文件配置

    转载自   http://condor.depaul.edu/glancast/374class/docs/csapp_compile_guide.html Compiling with the CS ...

  6. FastDFS的安装(复制自己用)

    FastDFS 安装及使用 FastDFS 安装及使用 2012-11-17 13:10:31|  分类: Linux|举报|字号 订阅     Google了一下,流行的开源分布式文件系统有很多,介 ...

  7. springMvc的400问题

    主要是参数类型对不上导致的 本文主要记录一些作者在使用spring mvc过程中遇到的一些以及解决办法,以备日后查询或者供其他网友阅读,每个问题的解决办法肯定不止一种,如果你也遇到过类似问题,并且有独 ...

  8. Mysql实战之主从复制的读写分离

    author:JevonWei 版权声明:原创作品 ProxySQL构建主从复制的读写分离 ProxySQL官网及下载地址 http://www.proxysql.com/ 架构角色 mysql-sl ...

  9. 【BZOJ 5047 空间传送装置】

    Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 282  Solved: 121[Submit][Status][Discuss] Descriptio ...

  10. 使用filter: blur() 的时候解决图片周围泛白和容器外范围变模糊的问题

    类似于这种,这个时候出现了周围变模糊,并且边缘泛白的情况 周围模糊这个问题很好解决,给父容器加overflow:hidden:就可以了 效果如上,至于周围泛白的问题就需要动点脑筋了,给目标添加 tra ...