44 Wildcard Matching

https://leetcode.com/problems/wildcard-matching/#/description

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

分析及代码

public boolean isMatch(String s, String p) {
    if(p == null) {
        return false;
    }
    int sl = s.length();
    int pl = p.length();
    int sp = 0;
    int pp = 0;
    int match = 0;
    int asterick = -1;
    while(sp < sl) {
        if(pp < pl && p.charAt(pp) == '*') {
            match = sp;
            asterick = pp++;
        } else if(pp < pl && (p.charAt(pp) == '?' || p.charAt(pp) == s.charAt(sp))) {
            sp++;
            pp++;
        } else if(asterick != -1) {
            sp = ++match;
            pp = asterick + 1;
        } else {
            return false;
        }
    }
    while(pp < pl && p.charAt(pp) == '*') {
        pp++;
    }
    return pp == pl;
}

例子:

"abefcdgiescdfimde"

"ab*cd?i*de"

results matching ""

    No results matching ""