121_leetcode_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

1:注意特殊情況;2:遇到‘?',分別++;3:遇到‘*’的情況



    bool isMatch(const char *s, const char *p)
    {
        if(s == NULL || p == NULL)
        {
            return false;
        }
        
        bool flag = false;
        const char* indexP = nullptr;
        const char* indexS = nullptr;
        
        while( *s != '\0')
        {
            if(*p == '?')
            {
                p++;
                s++;
            }
            else if(*p == '*')
            {
                while(*p == '*')
                {
                    p++;
                }
                p--;
                
                indexP = p;
                indexS = s;
                flag = true;
                p++;
            }
            else
            {
                if(*p == *s)
                {
                    p++;
                    s++;
                }
                else
                {
                    if(flag == true)
                    {
                        p = indexP + 1;
                        s = ++indexS;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        
        while(*p == '*')
        {
            p++;
        }
        
        if(*s == '\0' && *p == '\0')
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章