We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b655317 commit b3d3fefCopy full SHA for b3d3fef
27-needleInHaystack.js
@@ -0,0 +1,30 @@
1
+/**
2
+ * @param {string} haystack
3
+ * @param {string} needle
4
+ * @return {number}
5
+ */
6
+var strStr = function (haystack, needle) {
7
+ if (needle === "") {
8
+ return 0;
9
+ }
10
+
11
+ let arr = haystack.split("");
12
+ let target = needle.split("");
13
14
+ for (let i = 0; i <= arr.length - target.length; i++) {
15
+ if (arr[i] === target[0] && arr[i + target.length - 1]) {
16
+ let window = arr.slice(i, i + target.length);
17
+ if (
18
+ window.every((el, i) => {
19
+ return el === target[i];
20
+ })
21
+ ) {
22
+ return i;
23
24
25
26
27
+ return -1;
28
+};
29
30
+console.log(strStr("billthegiantman", "lt"));
0 commit comments