Skip to content

Commit cdef3a3

Browse files
committed
add solution
1 parent e596049 commit cdef3a3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} haystack
3+
* @param {string} needle
4+
* @return {number}
5+
*/
6+
var strStr = function (haystack, needle) {
7+
const slen = haystack.length
8+
const plen = needle.length
9+
if (slen == plen) {
10+
return haystack == needle ? 0 : -1
11+
}
12+
for (let i = 0; i <= slen - plen; i++) {
13+
let j
14+
for (j = 0; j < plen; j++) {
15+
if (haystack[i + j] != needle[j]) {
16+
break
17+
}
18+
}
19+
if (j == plen) return i
20+
}
21+
return -1
22+
};

0 commit comments

Comments
 (0)