Open In App

Palindrome Substring Queries in JavaScript

In this article, we will learn how can we find whether the given string is palindrome or not according to the given query. we are Given a string and several queries on the substrings of the given input string to check whether the substring is a palindrome or not.

Examples:

Input: “abaaabaaaba”, queries- [0, 10], [5, 8], [2, 5], [5, 9]
Output: palindrome, not a palindrome, not a palindrome, palindrome.
Explanation:
[0, 10] => Substring is “abaaabaaaba” which is a palindrome.
[5, 8] => Substring is “baaa” which is not a palindrome.
[2, 5] => Substring is “aaab” which is not a palindrome.
[5, 9] => Substring is “baaab” which is a palindrome.

These are the following approaches for solving this problem:



Approach 1: Using for loop

Example: In this example, we are checking Palindrome Substring Queries in JavaScript using For Loops.






let str = "abaaabaaaba";
let quer = [[0, 100], [5, 8], [2, 50], [5, 9]];
for (let query of quer) {
    let [s, e] = query;
    if (s < 0 || e >= str.length) {
        console.log(query, "Wrong Query. Invalid!");
        continue;
    }
    let sub = str.slice(s, e + 1);
    let palindrome = true;
    for (let i = 0; i < sub.length / 2; i++) {
        if (sub[i] !== sub[sub.length - 1 - i]) {
            palindrome = false;
            break;
        }
    }
    console.log(query, palindrome ? "Palindrome" : "Not a Palindrome");
}

Output
[ 0, 100 ] Wrong Query. Invalid!
[ 5, 8 ] Not a Palindrome
[ 2, 50 ] Wrong Query. Invalid!
[ 5, 9 ] Palindrome

Time Complexity: O(n * m). Where n is number of queries and m is the maximum substring length.

Space Complexity: O(1)

Approach 2: Using reverse(), join() and split() Methods

Example: In this example, we are checking Palindrome Substring Queries in JavaScript using reverse(), join(), and split() Methods.




let str = "abaaabaaaba";
let quer = [[0, 100], [5, 8], [2, 50], [5, 9]];
for (let query of quer) {
    let [s, e] = query;
    if (s < 0 || e >= str.length) {
        console.log(query, "Wrong Query. Invalid!");
        continue;
    }
    let sub = str.slice(s, e + 1);
    let output = sub.split('').reverse().join('');
    console.log(query, sub === output ? "Palindrome" : "Not a Palindrome");
}

Output
[ 0, 100 ] Wrong Query. Invalid!
[ 5, 8 ] Not a Palindrome
[ 2, 50 ] Wrong Query. Invalid!
[ 5, 9 ] Palindrome

Time Complexity: O(n * m). Where n is number of queries and m is the maximum substring length.

Space Complexity: O(n)

Approach 3: Using substring(), every() and charAt() Methods

Example: In this example, we are checking Palindrome Substring Queries in JavaScript using substring(), every(), and charAt() Methods.




let str = "abaaabaaaba";
let quer = [[0, 100], [5, 8], [2, 50], [5, 9]];
for (let query of quer) {
    let [s, e] = query;
    if (s < 0 || e >= str.length) {
        console.log(query, "Wrong Query. Invalid!");
    } else {
        let sub = str.substring(s, e + 1);
        let palindrome = [...sub]
        .every((char, index) => char ===
        sub.charAt(sub.length - 1 - index));
        console.log(query, palindrome ? "Palindrome" : "Not a Palindrome");
    }
}

Output
[ 0, 100 ] Wrong Query. Invalid!
[ 5, 8 ] Not a Palindrome
[ 2, 50 ] Wrong Query. Invalid!
[ 5, 9 ] Palindrome

Time Complexity: O(n * m). Where n is number of queries and m is the maximum substring length.

Space Complexity: O(n)


Article Tags :