44. Wildcard Matching (String; Recursion)

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

‘?‘ Matches any single character.
‘*‘ Matches any sequence of characters (including the empty sequence).不同于正则表达式中的*

*正则表达式的定义:

  • ‘.‘ Matches any single character.
  • ‘*‘ Matches zero or more of the preceding element.
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

思路:当遇到*,可能匹配0, 1, 2, ...个字符=>带回溯的递归

class Solution {
public:
    bool isMatch(string s, string p) {
        star = false;
        return recursiveCheck(s,p,0,0);
    }

    bool recursiveCheck(const string &s, const string &p, int sIndex, int pIndex){
        if(sIndex >= s.length()){
            while(p[pIndex] == ‘*‘ && pIndex < p.length()) pIndex++; //s has went to end, check if the rest of p are all *
            return (pIndex==p.length());
        }

        if(pIndex >= p.length()){
            return checkStar(s,p);
        }

        switch(p[pIndex]) //p: pattern,在p中才可能出现?, *
        {
        case ‘?‘:
            return recursiveCheck(s, p, sIndex+1, pIndex+1);
            break;
        case ‘*‘: //如果当前为*, 那么可认为之前的字符都匹配上了,并且将p移动到 * 结束后的第一个字符
            star = true;  //p 每次指向的位置,要么是最开始,要么是 * 结束的第一个位置
            starIndex = pIndex;
            matchedIndex = sIndex-1;
            while(p[pIndex] == ‘*‘&& pIndex < p.length()){pIndex++;} //忽略紧接在 *后面的*
            if(pIndex==p.length()) return true;//最后一位是*
            return recursiveCheck(s,p,sIndex,pIndex); //*匹配0个字符
            break;
        default:
            if(s[sIndex] != p[pIndex]) return checkStar(s, p);
            else return recursiveCheck(s, p, sIndex+1, pIndex+1);
            break;
         }
    }

    bool checkStar(const string &s, const string &p){
        if(!star) return false;
        else {
            int pIndex = starIndex+1;
            int sIndex = ++matchedIndex; //回溯,*d多匹配一个字符
            return recursiveCheck(s, p, sIndex, pIndex);
        }
    }
private:
    int starIndex;
    int matchedIndex;
    bool star;
};
时间: 2024-08-12 23:09:53

44. Wildcard Matching (String; Recursion)的相关文章

44. Wildcard Matching(js)

44. Wildcard Matching Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching

leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法

Wildcard Matching '?' 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,

44. 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 p

【一天一道LeetCode】#44. Wildcard Matching

一天一道LeetCode系列 (一)题目 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 par

(Java) LeetCode 44. Wildcard Matching —— 通配符匹配

Given an input string (s) and a pattern (p), 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 enti

19.2.4 [LeetCode 44] Wildcard Matching

Given an input string (s) and a pattern (p), 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 enti

44. Wildcard Matching *HARD*

'?' 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) Som

10. Regular Expression Matching &amp;&amp; 44. Wildcard Matching

10. Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial).

第八周 Leetcode 44. Wildcard Matching 水题 (HARD)

Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分匹配完成就算贪心了吧.. class Solution { public: bool match(string &s,string &p,int l2,int r2,int l1) { if(l2==r2)return true; if(l1+r2-l2-1>=s.length())re