[Written explanation will be added soon]
AFFILIATE LINKS
Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard
Here is the full implementation.
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
let strStr = function(haystack, needle) {
if (!needle) {
return 0;
}
let pointer2 = needle.length;
for (let pointer1 = 0; pointer1 < (haystack.length - needle.length) + 1; pointer1++) {
let substring = haystack.substring(pointer1, pointer2);
if (substring === needle) {
return pointer1;
}
pointer2++;
}
return -1;
};