https://leetcode.com/problems/implement-strstr/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
const strStr = (haystack, needle) => {
for(let i = 0; ; i++) {
for(let j =0; ; j++) {
if (j === needle.length) {
return i
}
if (i + j === haystack.length) {
return -1
}
if (haystack[i + j] !== needle[j]) {
break
}
}
}
};