L
L
Ler Den2019-07-29 00:12:26
JavaScript
Ler Den, 2019-07-29 00:12:26

Is it possible to search for palindromes in a huge js text in a browser?

Hello everyone, such a question.
There is a requirement to implement a search for palindrome words ("madam") and palindrome sentences ("tree leaves leaves tree") in arbitrarily large text that the user enters. The text can be at least the entire "war and peace", even the entire British Encyclopedia. And in this text it is necessary to find the longest n palindromes.
For example, a palindrome can be a text that contains 74,633 letters and the script must also determine it.
Below I attach what I roughly got for searching for palindromes-words (for searching for palindromes-sentences it differs quite a bit), but the complexity of the algorithm is O (n ^ 2) and it hangs already at 500 letters (500*500 = 199809 iterations).
Even if we assume that the complexity of the algorithm is O(n), it will hang at 200k letters, which is clearly less than the content of "war and peace".
How adequate is it to require to implement on JS? And how feasible is this?

let lookFor = (arr) => {
        const length = arr.length;
        let resultSet = new Set();

        for (let i = 0; i < length; i++) {
            for (let j = 0; j < length; j++) {
                let subArr = arr.slice(i, j + 1);
                if (isPalindrome(subArr)) {
                    let palindromeStr = stripSpecial(subArr.join(' '));
                    resultSet.add(palindromeStr);
                }
            }
        }
        return resultSet;
    }

let isPalindrome = (subArr) => {
        // remove all special characters
        let re = /[\`\~\!\@\#\$\%\^\:\;\&\?\*\(\)\_\-\=\+\,\[\]\|\/\'\"\.]/g;
        let str = subArr.join(' ').toLowerCase().replace(re, '');
        str = str.replace(/ /g, '');
        var len = str.length;
        for (var i = 0; i < len / 2; i++) {
                // as long as the characters from each part match, the FOR loop will go on
                if (str[i] !== str[len - 1 - i]) {
                    return false;
                }
        }
        // both parts are strictly equal, it returns true => The string is a palindrome
        return true;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
strelok011, 2019-07-29
@strelok011

Theoretically, it can be done. In the forehead - will not work. It is necessary to apply engineering solutions for bigdata, optimization, perhaps task clustering, and so on. Those. it will not be possible to disassemble this volume at once. Not enough memory and performance.

Y
Yaroslav Rul, 2019-07-29
@SpaceX_1

isPalindrome = (string) => {
  string = string.toLocaleLowerCase();
  return Array.from(string).toString() === Array.from(string).reverse().toString()
}

This is the code for isPalindrome. You have too much cyclomatic complexity, avoid this amount of nesting.

T
ThunderCat, 2019-07-29
@ThunderCat

a couple of notes:
// remove all special characters - probably doing it once for the whole text will be more efficient than pulling it every time in a loop.

function reverseString(str) {
    return str.split("").reverse().join("");
}
most likely (not the fact) it will work faster to expand the entire string and check for equality.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question