LeetCode-Wildcard Matching

来源:互联网 发布:h5手机页面源码下载 编辑:程序博客网 时间:2024/06/13 09:23

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
Solution:

Code:

<span style="font-size:14px;">class Solution {public:    bool isMatch(const char *s, const char *p) {        int indexS = 0, indexP = 0, preS = -1, preP = -1;        while (s[indexS] != '\0') {            if (s[indexS] == p[indexP] || p[indexP] == '?') {                ++indexS;                ++indexP;                continue;            }            if (p[indexP] == '*') {                preS = indexS;                preP = indexP;                ++indexP;                continue;            }            if (preP != -1) {                ++preS;                indexS = preS;                indexP = preP+1;                continue;            }            return false;        }        while (p[indexP] != '\0' && p[indexP] == '*') ++indexP;        return p[indexP] == '\0';    }};</span>



0 0
原创粉丝点击