My simple Javascript solution


  • 0
    K
    var strStr = function(haystack, needle) {
    if(!needle &&! haystack)return 0;
    if(  !haystack && needle)return -1;
    var lh=haystack.length;
    var ln=needle.length;
    for(i=0;i+ln-1 < lh;i++)//abcdefg   cde
    {
        var res=haystack.substr(i, ln);
        if(res.localeCompare(needle)==0)return i;
    }
    return -1;
    

    };


Log in to reply
 

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