My Javascript solution, quite simple


  • 0
    J
    var strStr = function(haystack, needle) {
        var hlen = haystack.length;
        var nlen = needle.length;
        
        for(var i=0;i<hlen-nlen+1;i++){
            var substr = haystack.substr(i, nlen);
            if(substr.valueOf() === needle.valueOf()){
                return i;
            }
        }
        return -1;
    };

  • 1
    A

    public int strStr(String haystack, String needle) {
    return haystack.indexOf(needle);

    }

Log in to reply
 

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