Answers for "brute force search javascript"

0

brute force search javascript

function bruteForceSubstringSearch(text, pattern) {
    const lengthOfText = text.length;
    const lengthOfPattern = pattern.length;
    for (let i = 0; i < lengthOfText; i++) {
        let j;
        for (j = 0; j < lengthOfPattern; j++) {
            if (text.charAt(i + j) !== pattern.charAt(j)) {
                   break;
            }
        }
        if (j === lengthOfPattern) return i;
    }
    return lengthOfText;
}

bruteForceSubstringSearch('this is a test', 'test');
Posted by: Guest on May-31-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language