I have implemented the naive String search problem but I want to know what can be the alternate way to solve this algorithm ?
Here is my example :-
function naiveSearch(long, short) {
let count = 0;
for (let i=0;i<long.length;i++) {
for(let j=0;j<short.length;j++) {
if (short[j] !== long[i+j])
break;
if (j === short.length - 1)
count++;
}
}
return count;
}
As you can see the Time Complexity of this code is O(n ^ 2) as i have used the nested loop. So what can be the another way to solve this algorithm which will reduced the Time complexity ?