Solution


  • 0
    L
    // LeetCode, Wildcard Matching
    class Solution {
    public:
    bool isMatch(const string& s, const string& p) {
    return isMatch(s.c_str(), p.c_str());
    }
    private:
    bool isMatch(const char *s, const char *p) {
    bool star = false;
    const char *str, *ptr;
    for (str = s, ptr = p; *str != '\0'; str++, ptr++) {
    switch (*ptr) {
    case '?':
    break;
    case '*':
    star = true;
    s = str, p = ptr;
    while (*p == '*') p++;
    //skip continuous '*'
    if (*p == '\0') return true;
    str = s - 1;
    ptr = p - 1;
    break;
    default:
    if (*str != *ptr) {
    if (!star) return false;
    s++;
    str = s - 1;
    ptr = p - 1;
    }
    }
    }
    while (*ptr == '*') ptr++;
    return (*ptr == '\0');
    }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.