-
Notifications
You must be signed in to change notification settings - Fork 12
/
naive-search.js
38 lines (33 loc) · 1.06 KB
/
naive-search.js
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
/**
* Naive Search
*
* Starts at the beginning and iterates through the whole string
*
* Best case performance: Ω(1) (the substring you're looking for is the start of the string)
* Average case performance: 0(n+m)
* Worst case performance: O(n*m), where the length of the pattern is m and the length of the search string is n
* (the substring you're looking for is the end of the string or not in the string at all)
*/
export const naiveSearch = (haystack, needle, showLogs) => {
if (typeof haystack !== 'string' || typeof needle !== 'string') {
/* istanbul ignore next */
showLogs && console.log('bad input, exiting early')
return -1
}
if (needle.length > haystack.length) {
/* istanbul ignore next */
showLogs && console.log('needle is longer than the haystack, exiting early')
return -1
}
for (let i = 0; i < haystack.length; i++) {
for (let j = 0; j < needle.length; j++) {
if (haystack[i + j] !== needle[j]) {
break
}
if (j === needle.length - 1) {
return i
}
}
}
return -1
}