Search for substring using JavaScript function
P粉098979048
P粉098979048 2023-09-13 11:04:31
0
1
315

I want to write a code in JavaScript language that can search for the number of repetitions of letters in a word, like this code, but in a shorter way o(n).

function naiveSearch(long, short){
    var count = 0;
    for(var i = 0; i < long.length; i++){
        for(var j = 0; j < short.length; j++){
           if(short[j] !== long[i+j]) break;
           if(j === short.length - 1) count++;
        }
    }
    return count;
}
naiveSearch("lorielol loled", "lol")

P粉098979048
P粉098979048

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!