-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.strstr.js
More file actions
59 lines (53 loc) · 1.45 KB
/
string.strstr.js
File metadata and controls
59 lines (53 loc) · 1.45 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
/*
* @title: implement atoi in javascript
* @description: Return the index of the first occurrence of
* needle in haystack, or -1 if needle is not part of haystack.
* @author: Thorsten Kober
* @email: [email protected]
*/
function strStr1(haystack, needle) {
if (haystack === needle || needle === '') {
return 0;
}
for (let i = 0; i < haystack.length; i++) {
if (haystack[i] === needle[0]) {
const temp = haystack.substring(i, i + needle.length);
if (temp === needle) {
return i;
}
}
}
return -1;
}
function strStr2(haystack, needle) {
const needleLength = needle.length;
if (needle === '' && haystack === '') {
return 0;
}
for (let i = 0; i < haystack.length; i++) {
if (haystack.substr(i, needleLength) === needle) {
return i;
}
}
return -1;
}
function strStr3(haystack, needle) {
if (needle === '') return 0;
const split = haystack.split(needle);
return split.length > 1 ? split[0].length : -1;
}
// npx jest algorithms/string/string.strstr.js
describe('implement strStr', () => {
test('strStr1()', () => {
expect(strStr1('hello', 'll')).toEqual(2);
expect(strStr1('aaaaaaaa', 'bb')).toEqual(-1);
});
test('strStr2()', () => {
expect(strStr2('hello', 'll')).toEqual(2);
expect(strStr2('aaaaaaaa', 'bb')).toEqual(-1);
});
test('strStr3()', () => {
expect(strStr3('hello', 'll')).toEqual(2);
expect(strStr3('aaaaaaaa', 'bb')).toEqual(-1);
});
});