Open In App

Palindrome Substring Queries in JavaScript

Last Updated : 22 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • In this approach, we have used the for loop to iterate over the set of queries on the substrings of the input string.
  • We are Iterating through queries, extracting substrings, and checking if each substring is a palindrome by comparing characters from both ends.

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

Javascript




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

  • In this approach, we extract the substring from the input string using the split() method.
  • Then by using the reverse() method we reverse the order of elements in the array. join() method joins the reverse back into the string without any separator.

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

Javascript




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

  • In this approach, we extract substrings using substring(), convert them to an array.
  • Using every() to check if all characters match their mirrored positions in the substring.

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

Javascript




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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads