Sentence Palindrome (original) (raw)

Last Updated : 14 Jul, 2025

Given a sentence **s, determine whether it is a **palindrome sentence or not. A palindrome sentence is a sequence of characters that reads the same forward and backward after:

**Examples:

**Input: s = "Too hot to hoot."
**Output: true
**Explanation: If we remove all non-alphanumeric characters and convert all uppercase letters to lowercase, string s will become "toohottohoot" which is a palindrome.

**Input: s = "Abc 012..## 10cbA"
**Output: true
**Explanation: If we remove all non-alphanumeric characters and convert all uppercase letters to lowercase, string s will become "abc01210cba" which is a palindrome.

**Input: s = "ABC $. def01ASDF.."
**Output: false
**Explanation: If we remove all non-alphanumeric characters and convert all uppercase letters to lowercase, string s will become "abcdef01asdf" which is not a palindrome.

Try It Yourselfredirect icon

Table of Content

[Naive Approach] Normalization and Reverse Check - O(n) Time and O(n) Space

The idea is to preprocess the given sentence by filtering out all non-alphanumeric characters and converting all uppercase letters to lowercase. This ensures that the comparison is not affected by spaces, punctuation, or letter casing. Once the string is normalized, we simply compare it with its reverse. If both are identical, the sentence is a palindrome; otherwise, it is not.

C++ `

#include #include using namespace std;

bool isPalinSent(string &s) {

// create a new string having only
// alphanumeric characters
string s1 = "";
for (char ch : s) {
    if (isalnum(ch)) {
        s1.push_back(tolower(ch));
    }
}

// find reverse of this new string
string rev = s1;
reverse(rev.begin(), rev.end());

// compare string and its reverse
return s1 == rev;

}

int main() { string s = "Too hot to hoot.";

cout << (isPalinSent(s) ? "true" : "false") << endl;
return 0;

}

Java

class GfG { static boolean isPalinSent(String s) {

    // create a new string having only 
    // alphanumeric characters
    StringBuilder s1 = new StringBuilder();
    for (char ch : s.toCharArray()) {
        if (Character.isLetterOrDigit(ch)) {
            s1.append(Character.toLowerCase(ch));
        }
    }

    // find reverse of this new string
    StringBuilder rev = new 
            StringBuilder(s1.toString());
    rev.reverse();

    // compare string and its reverse
    return s1.toString().equals(rev.toString());
}

public static void main(String[] args) {
    
    String s = "Too hot to hoot.";
    System.out.println(isPalinSent(s)
                                   ? "true" : "false");
}

}

Python

def isPalinSent(s): # create a new list having only # alphanumeric characters s1 = [] for ch in s: if ch.isalnum(): s1.append(ch.lower())

# convert the new list to string
s1 = ''.join(s1)

# find reverse of this new string
rev = s1[::-1]

# compare string and its reverse
return s1 == rev

if name == "main": s = "Too hot to hoot." print("true" if isPalinSent(s) else "false")

C#

using System; using System.Text; using System.Linq;

class GfG { static bool isPalinSent(string s) { StringBuilder s1 = new StringBuilder();

    // Filter out non-alphanumeric characters and 
    // convert to lowercase
    foreach (char ch in s) {
        if (Char.IsLetterOrDigit(ch)) 
            s1.Append(Char.ToLower(ch));
    }
    
    string s2 = s1.ToString();
  
      // find reverse of this new string
    char[] revArray = 
            s2.ToCharArray().Reverse().ToArray();
    string rev = new string(revArray);
  
      // compare string and its reverse
    return s2 == rev;
}

static void Main() {
    string s = "Too hot to hoot.";
    Console.WriteLine(isPalinSent(s) ? "true" : "false");
}

}

JavaScript

function isPalinSent(s) { // create an array to store only // alphanumeric characters let s1 = []; for (let i = 0; i < s.length; i++) { let ch = s[i];

    // Check if the character is alphanumeric
    if (/[a-zA-Z0-9]/.test(ch)) {
        s1.push(ch.toLowerCase());
    }
}
// find reverse of this new string by 
// joining the array into a string
let rev = s1.slice().reverse().join("");

// compare string and its reverse
return s1.join("") === rev;

}

// Driver Code let s = "Too hot to hoot."; console.log(isPalinSent(s) ? "true" : "false");

`

[Expected Approach] Using Two Pointers - O(n) Time and O(1) Space

The idea is to use the two-pointer approach to check if a sentence is a palindrome. We place one pointer at the start and the other at the end of the string, moving them toward each other while comparing characters.
We skip any non-alphanumeric characters and convert uppercase letters to lowercase to ensure a case-insensitive comparison. If the characters at both pointers don't match, we return false. If the pointers cross without a mismatch, the sentence is a palindrome.

C++ `

#include using namespace std;

bool isPalinSent(string s) { int i = 0, j = s.length() - 1;

// Compares character until they are equal
while (i < j) {

    // Skip non alphabet character
    // from left side
    if (!isalnum(s[i]))
        i++;

    // Skip non alphabet character
    // from right side
    else if (!isalnum(s[j]))
        j--;

    // If characters are equal
    // update the pointers
    else if (tolower(s[i]) == tolower(s[j]))
        i++, j--;

    // If characters are not equal then
    // sentence is not palindrome
    else
        return false;
}

return true;

}

int main() { string s = "Too hot to hoot.";

cout << (isPalinSent(s) ? "true" : "false") << endl;
return 0;

}

C

#include <stdio.h> #include <stdbool.h>

bool isPalinSent(const char* s) { int i = 0, j = strlen(s) - 1;

// Compares character until they are equal
while (i < j) {

    // Skip non alphabet character
    // from left side
    if (!isalnum(s[i]))
        i++;

    // Skip non alphabet character
    // from right side
    else if (!isalnum(s[j]))
        j--;

    // If characters are equal 
    // update the pointers
    else if (tolower(s[i]) == tolower(s[j]))
        i++, j--;

    // If characters are not equal then
    // sentence is not palindrome
    else
        return false;
}

return true;

}

int main() { const char* s = "Too hot to hoot."; printf("%s\n", isPalinSent(s) ? "true" : "false"); return 0; }

Java

class GfG {

static boolean isPalinSent(String s) {
    int i = 0, j = s.length() - 1;

    // Compares character until they are equal
    while (i < j) {

        // Skip non alphabet character from left side
        if (!Character.isLetterOrDigit(s.charAt(i)))
            i++;

        // Skip non alphabet character from right side
        else if (!Character.isLetterOrDigit(s.charAt(j)))
            j--;

        // If characters are equal update the pointers
        else if (Character.toLowerCase(s.charAt(i)) 
                == Character.toLowerCase(s.charAt(j))) {
            i++;
            j--;
        }
      
        // If characters are not equal then sentence
        // is not palindrome
        else
            return false;
    }

    return true;
}

public static void main(String[] args) {
    String s = "Too hot to hoot.";
    System.out.println(isPalinSent(s) 
                       ? "true" : "false");
}

}

Python

def isPalinSent(s): i, j = 0, len(s) - 1

# Compares character until they are equal
while i < j:

    # Skip non alphabet character
    # from left side
    if not s[i].isalnum():
        i += 1

    # Skip non alphabet character
    # from right side
    elif not s[j].isalnum():
        j -= 1

    # If characters are equal 
    # update the pointers
    elif s[i].lower() == s[j].lower():
        i += 1
        j -= 1

    # If characters are not equal then
    # sentence is not palindrome
    else:
        return False

return True

if name == "main": s = "Too hot to hoot." print("true" if isPalinSent(s) else "false")

C#

using System;

class GfG {

static bool isPalinSent(string s) {
    int i = 0, j = s.Length - 1;

    // Compares character until they are equal
    while (i < j) {

        // Skip non alphabet character
        // from left side
        if (!char.IsLetterOrDigit(s[i]))
            i++;

        // Skip non alphabet character
        // from right side
        else if (!char.IsLetterOrDigit(s[j]))
            j--;

        // If characters are equal 
        // update the pointers
        else if (char.ToLower(s[i]) == char.ToLower(s[j])) {
            i++;
            j--;
        }

        // If characters are not equal then
        // sentence is not palindrome
        else
            return false;
    }

    return true;
}

static void Main() {
    string s = "Too hot to hoot.";
    Console.WriteLine(isPalinSent(s) ? "true" : "false");
}

}

JavaScript

function isPalinSent(s) { let i = 0, j = s.length - 1;

// Compares character until they are equal
while (i < j) {

    // Skip non alphabet character
    // from left side
    if (!s[i].match(/[a-zA-Z0-9]/))
        i++;

    // Skip non alphabet character
    // from right side
    else if (!s[j].match(/[a-zA-Z0-9]/))
        j--;

    // If characters are equal
    // update the pointers
    else if (s[i].toLowerCase() === s[j].toLowerCase())
        i++, j--;

    // If characters are not equal then
    // sentence is not palindrome
    else
        return false;
}

return true;

}

// Driver Code let s = "Too hot to hoot."; console.log(isPalinSent(s) ? "true" : "false");

`