OJ returns error but when running the code in compiler it returns the correct result for input "aaaabbbababb", "baa"


  • 0
    C
    char* strStr(char* haystack, char* needle){
    char* current;
    char* startOfMatch;
    char* match;
    char empty[] = "";
    
    if (NULL == haystack || NULL == needle)
        return empty;
    
    current = haystack;
    match = needle;
    
    if ('\0' == *match)
        return current;
    
    while ('\0' != *current){
        if (*current == *match){
            startOfMatch = current;
    
            while ('\0' != *match && '\0' != *current){
                if (*current != *match){
                    match = needle;
                    current = startOfMatch;
                    break;
                }
    
                ++current;
                ++match;
            }
    
            if ('\0' == *match){
                return startOfMatch;
            }            
        }
    
        ++current;
    }
    
    if (('\0' == *current) && ('\0' != *match)){
        return NULL;
    }
    
    return empty;
    

    }


Log in to reply
 

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