Weird input case causes error any help?


  • 2
    R

    bool isMatch(char* s, char* p) {

     char* ss;
     
     while( (*p != '\0') && *p == '*'){
         p++;
     }
     
     
     if(*s == '\0'){
         return *p == '\0';
     }
     
     if( (*p == '\0') && (*s != '\0') ){
         return false;
     }
     
     //parse p
     
     
     
     if( ( *(p+1) != '*' ) && ( *p == '.' ||  isalpha(*p) ) ){
         return (*p == *s || *p == '.') && isMatch(s+1, p+1);
     }
     
     //consider the most complicated case
     if(*p != '.'){
         if(*p != *s){
             return isMatch(s, p + 2);
         }
     }
     
     for(ss = s; *ss != '\0' && *ss == *s; ss++){
         
         if(isMatch(ss, p + 2)){
             return true;
         }
     }
     
     return isMatch(ss, p + 2);
    

    }

    My code is in c version.

    But, it pops an error with input "aa".
    I do think IsMatch function should have two input parameters ?


  • 0
    J

    me too. same problem. I also meet this problem using c++. Does any one meet this problem?


  • 0
    Q

    Same here with python input


  • 0
    R

    bool isMatch(char* s, char* p) {

     if( (*p) == '\0') return (*s) == '\0';
     
     if( ( *(p+1) != '*' ) ){
         if( (*p) == (*s) || (*p) == '.' && (*s) != '\0'){ 
           return  isMatch(s+1, p+1);
         }else{
             return false;
         }
     }else{
         
        while(*p == *s || ( (*p) == '.' && (*s) != '\0') ){
             if(isMatch(s, p + 2)){
                 return true;
             }
             
             s++;
         }
         
         return isMatch(s, p + 2);
     }
    

    }

    I updated the solution as the above. Now, everything is going to be all right. I can not figured out what is the difference between two solutions.


  • 0
    D

    Same here with python input "ab".


Log in to reply
 

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