Wildcard Matching

来源:互联网 发布:ipad怎么看淘宝直播间 编辑:程序博客网 时间:2024/06/06 00:35

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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false

class Solution {public:    bool isMatch(string s, string p) {        int ssize = s.size();        int psize = p.size();                int i = 0, j = 0, pi = 0, pj = psize;        while (i < ssize)        {            if (j < psize && (s[i] == p[j] || p[j] == '?'))            {                i++;                j++;                continue;            }            else if (j < psize && p[j] == '*')            {                pj = j++;                pi = i;                continue;            }            else if (pj < psize)            {                j = pj + 1;                i = ++pi;                continue;            }            return false;        }        while (j < psize && p[j] == '*')        {            j++;        }                return j == psize;    }};


0 0
原创粉丝点击