Different output from OJ and VS2012


  • 0
    G

    I have a non-recursive solution to this problem. It works when I run on VS2012 (at least for the test cases I tried), but it gives different output for some cases, such as "aa", "a*".

    could anyone look at the following code, and figure out the errors there? Thanks.

    class Solution {
    public:
    bool isMatch(const char *s, const char *p) {
    	if (s == NULL || p == NULL) return false;
    
    	const char *pstar = NULL;
    	const char *pss = NULL;
    
    	while (*s || *p) {
    		if (*s == *p || (*p == '.' && *s)) { // match found, move to next
    			s++;
    			p++;
    		}
    		else if (*p == '*') {
    			pstar = p;
    			pss = s;
    			s = s - 1;  // * match zero characters
    			p = p + 1;
    		}
    		else if (*(p + 1) == '*') {
    			p = p + 2;
    		}
    		else if (pstar && (*(pstar - 1) == *(pss - 1) || (*(pstar - 1) == '.') && *s!='\0')){
    			s = pss;
    			pss++;
    			p = pstar + 1;
    		}
    		else {
    			return false;
    		}
    	}
    	return true;
    }
     };

  • 0
    S

    Could you please update your post with some explanation of your algorithm, and make some comment in your code?


  • 0
    S

    I have no idea what your algorithm is, so do not know if it right.

    However I could tell now, when run the test case "aa", "a*", your *p will go out of string range, you can simulate by yourself on paper. so there is no guarantee about what result will return. Maybe it leads to different judgement between local and LeetCode OJ.


Log in to reply
 

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