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. (JAVA指针),对象引用问题

    引出指针 从表面上看JAVA是没有指针的,或者是说,弱化了指针.但是指针在JAVA中还是真真切切存在的.在Java中我们称之为引用. String a;//引用为空 String a  = new S ...

  2. [2016-04-19 15:46:03 - IceHoloReader1.0] Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER [20

    [2016-04-19 15:46:03 - IceHoloReader1.0] Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER [20 ...

  3. Linux QA

    gitee: https://gitee.com/dhclly/icedog.script.test/blob/master/doc/linux/linux-qa.md 1. linux 中的 ll( ...

  4. Install ADDS on Windows Server 2012 R2 with PowerShell

    Install ADDS on Windows Server 2012 R2 with PowerShell Posted by ethernuno on 20/04/2014 In this tut ...

  5. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  6. 66、fastJson 解析json数据时,如果key值不同怎么处理?

    在某些场景,你可能需要定制序列化输出,比如说,希望序列化采用之后采用"ID",而不是"id",你可以使用@JSONField这个Annotation. publ ...

  7. Python-S9—Day86-ORM项目实战之会议室预定相关

    01 会议室预定1 02 会议室预定2 03 会议室预定3 04 会议室预定4 05 会议室预定5 06 会议室预定6 01 会议室预定1 1.1 项目的架构目录: 1.2 使用Pycharm创建Dj ...

  8. sed处理大txt文件(1G) 比如替换某一串字符串,或者删除一行

    1.将11.sql文件中"prompt"替换为"--prompt",然后保存为111.sql文件 sed -e "s,prompt,--prompt, ...

  9. java面试需要准备什么呢?c++可以看看

    作者:Mingche Su链接:https://zhuanlan.zhihu.com/p/20545626来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 语言知识点:以 ...

  10. 监视网络接口TCP状态信息数据有多种工具或命令。下面举例一些:

    nstat命令 nstat kernel ======= ss -s == netstat -i netstat -s ip -s link sar -n DEV 1