3 ms C solution with BF-improved algorithm


  • 1

    more codes see: https://github.com/lightmen/leetcode.git

    int strStr(char* haystack, char* needle) {

    int jump[256];
    int nd_len = strlen(needle);
    int ht_len = strlen(haystack);
    int i,j;
    
    for(i = 0; i < 256; ++i)
        jump[i] = nd_len + 1;
    
    for(i = 0; i < nd_len; ++i)
        jump[needle[i]] = nd_len - i;
        
    for(i = 0; i <= ht_len - nd_len; i += jump[haystack[i+nd_len]]){
        j = 0;
        while(j < nd_len && needle[j] == haystack[i+j])
            ++j;
    
        if(j == nd_len)
            return i;
    }
    
    return -1;
    

    }


  • 0
    I

    if there is unicode characters in needle or haystack ?


Log in to reply
 

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