-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement-strStr().js
More file actions
62 lines (56 loc) · 2.02 KB
/
implement-strStr().js
File metadata and controls
62 lines (56 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Implement strStr() or indexOf().
// Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
// Example 1:
// Input: haystack = "hello", needle = "ll"
// Output: 2
// Example 2:
// Input: haystack = "aaaaa", needle = "bba"
// Output: -1
console.log(strStr("aaaaa", "bba"),"Expected:", -1);
console.log(strStr("hello", "ll"),"Expected:", 2);
console.log(strStr("", ""),"Expected:", 0);
console.log(strStr("aaa", "a"),"Expected:", 0);
console.log(strStr("aaa", "aa"),"Expected:", 0);
console.log(strStr("aaa", "aaaa"),"Expected:", -1);
console.log(strStr("mississippi", "issip"),"Expected:", 4);
console.log(strStr("mississippi", "issipi"),"Expected:", -1);
console.log(strStr("mississippi", "pi"),"Expected:", 9);
function strStr(haystack, needle) {
let haystackArray = haystack.split('');
let needleArray = needle.split('');
let returnValue = 0;
let accumValue = 0;
if (needle === "") {
return returnValue = 0;
} else if (haystack === needle) {
return returnValue = 0;
} else if (needle.length > haystack.length) {
return returnValue = -1;
}
findFirstIndex();
function findFirstIndex(sliceIndex) {
let newHaystackArray = haystackArray;
if (sliceIndex) {
newHaystackArray = haystackArray.slice(sliceIndex);
} else {
newHaystackArray = haystackArray;
}
if (newHaystackArray.length < needleArray.length) {
accumValue = 0;
return returnValue = -1;
}
returnValue = newHaystackArray.indexOf(needleArray[0]);
if (returnValue === -1) {
return returnValue;
}
for (i = 0; i < needleArray.length; i++) {
if (newHaystackArray[returnValue + i] === needleArray[i]){
} else {
accumValue += returnValue + 1;
findFirstIndex(accumValue);
break;
}
}
}
return returnValue + accumValue;
};