28. Find the Index of the First Occurrence in a String
202409012112
tags: #string
var strStr = function(haystack, needle) {
for (let i = 0; i <= haystack.length - needle.length; i++) {
if (haystack.slice(i, i + needle.length) === needle) {
return i;
}
}
return -1;
};
Reference
https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/