Why it is giving "Runtime Error" , but code runs fine on my local machine. Sample input is ("abce", "abc*d")


  • 0
    C

    class Solution {

    public:

    bool regex(const char *s, const char *p) {
    	if(strlen(s)==0 && strlen(p)==0){
            return true;
        } else if(strlen(s)!=0 && strlen(p)==0){
            return false;
    	} else if(strlen(s)==0 && strlen(p)==1 && p[0]=='*'){
    		return true;
    	}
    	
    	if(p[0] == '?' || s[0] == p[0]){
    		return regex(s+1, p+1);
    	} else if (p[0]=='*'){
    		return regex(s, p+1) || regex(s+1, p);
    	} else {
    		return false;
    	}
            
    }
    
    bool isMatch(const char *s, const char *p) {
        
        // Remove all consecutive "*"
        string str(s);
        string str2;
        str2 = str[0];
        int j = 0;
        
        for(unsigned int i=1; i<str.length(); i++){
    		if(str2[j] == '*' && str[i] == str2[j]){
    			;
    		} else {
    			j++;
    			str2 += str[i];
    		}
    	}
    	
    	return regex(s, p);
    }
    

    };


  • 0
    K

    check for the following test case:
    s = ""
    p = "*d"

    it satisfies the condition:
    if (p[0]=='*')
    and call to regex(s+1, p) will cause the problem, because you are going beyond string s.


Log in to reply
 

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