44.Wildcard Matching

1-Hard

https://leetcode.com/problems/wildcard-matching/discuss/17810

tonygogogo

class Solution {
    public boolean isMatch(String string, String pattern) {
        int i = 0;
        int j = 0;
        char[] s = string.toCharArray();
        char[] p = pattern.toCharArray();
        int lenS = s.length;
        int lenP = p.length;

        int lastMatch = - 1;
        int startJ = -1;

        while(i < lenS){
            if(j < lenP && (s[i] == p[j] || p[j] == '?')){
                i++;
                j++;
            }
            else if (j < lenP && p[j] == '*'){
                startJ = j;
                j++;
                lastMatch = i;
            }
            else if(startJ != -1){
                j = startJ + 1;
                lastMatch++;
                i = lastMatch;
            }
            else
                return false;
        }
        while( j < lenP && p[j] == '*')
            j++;
        return j == lenP;
    }
}

results matching ""

    No results matching ""