Vs2010 pass, but OJ is WA


  • 0
    L
    map<pair<int, int>, bool> match;
    bool isMatch(const char *s, const char *p) {
        return Match(s, 0, p, 0);
    }
    bool Match(const char *s, int x, const char *p, int y){
        int len1 = strlen(s), len2 = strlen(p);
        if(len1 == 0 && len2 == 0)
            return true;
        if(len2 < 1 || x > len1 || y > len2)
            return false;
        if(match.find(make_pair(x, y)) != match.end())
            return match[make_pair(x, y)];
            
        int j = x, i = y, k;
        for(i = y; i < len2 && j < len1; ++i)
        {
            char c = p[i];
            if(c == '*'){
                for(k = 0; k <= len1-j; ++k)
                {
                    if(j+k < len1 && i+1 < len2)
                    {
                        pair<int, int> pr(j+k, i+1);
                        match[pr] = Match(s, j+k, p, i+1);
                        if(match[pr])
                            return true;
                    }else if(j+k == len1 && i+1 == len2){
                        return true;
                    }
                }
                return false;
            }else if(c == '?'){
                ++j;
            }else{
                if(c != s[j])
                    return false;
                else
                    ++j;
            }
        }
        while(i < len2 && p[i] == '*')
            ++i;
        if(i < len2 || j < len1)
            return false;
        else
            return true;
    }
    

    My code passed in vs2010 at the test case: "c", "?", but get a WA in OJ
    Input: "c", "?"
    Output: false
    Expected: true
    I checked my code many times, but I cannot found the error, can anyone help me to find some error in my code?


Log in to reply
 

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