-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path03-Search-Sub-String-Occurances-In-String.js
43 lines (39 loc) · 1.16 KB
/
03-Search-Sub-String-Occurances-In-String.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
39
40
41
42
43
/**
* Find the number of occurances of a sub string in a string.
*
* @author Aditya Hajare <https://github.com/aditya43>
*
* IMPORTANT POINTS AND PSUDOCODE
* -----------------------------------
* 1. Loop over the longer string
* 2. Loop over the shorter string
* 3. If the characters don't match, break out of the inner loop
* 4. If the characters do match, keep going
* 5. If you complete the inner loop and find a match, increment the
* count of matches
* 6. Return the count
*
* @param str String
* @param val Sub string to search
*/
function naiveSearch (str, val) {
if (str.length < 1 || val.length < 1) {
return 0;
}
let occurances = 0;
for (let i = 0; i < str.length; i++) {
for (let j = 0; j < val.length; j++) {
if (val[j] !== str[i + j]) {
break;
}
if (j === val.length - 1) {
occurances++;
}
}
}
return occurances;
}
console.log(naiveSearch('Hello Adi, Hi Aditya', 'Adi'));
console.log(naiveSearch('Hello Adi, Hi Aditya', 'Nishi'));
console.log(naiveSearch('Hello Adi, Hi Aditya', 'Aditya'));
console.log(naiveSearch('', 'Adi'));