(Leetcode problem )What is the logical difference in these Implementations

This is the leetcode problem link
I am confused about the difference in the implementation,
1st Implementation is:
int strStr(string haystack, string needle) {

    int needleSize = needle.length();
    if(needleSize==0) return 0;
    int i=0,j=0;
    while(i<haystack.length() && j<needleSize){
        if(haystack[i] == needle[j])
            j++;
        else if(j>0){
            //Move back pointer i if mismatch
            i = i - j;
            j=0;
        }
        i++;
        if(j==needleSize)
            return i-j;   
    }
    return -1;
}

and the 2nd Implementation is:

    int strStr(string haystack, string needle) {
    int index=-1;
    int n=haystack.length(); int m=needle.length();
    for(int i=0;i<n;i++){
        if(haystack[i]==needle[0]){
            int tmpindex=i; int j=0;
            while(i<n&&j<m&&haystack[i]==needle[j]){
                j++,i++;
            }
            if(j==m){
                index=tmpindex;
                break;
            } 
            else i=tmpindex;
        }
    }
    if(m==0) index=0;
    return index;
}

2nd is giving me TLE, while 1st one is AC, what is the difference excatly?