brute-force solution in C


  • 0
    S
    int strStr(char* haystack, char* needle) {
        int i, j, mark=1;
        int hlen = strlen(haystack);
        int nlen = strlen(needle);
        
        if(hlen==0 && hlen==nlen)   return 0;
        
        for(i=0; i<hlen; i++){
            mark=1;
            for(j=0; j<nlen; j++){
                if(i+j < hlen){
                    mark &= (int) haystack[i+j] == needle[j];
                }else{
                    return -1;
                }
            }
            
            if(mark)    return i;
        }
        
        return -1;
    }
    

Log in to reply
 

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