1.

A sentence is said to be a palindrome if we convert all its alphabets to lowercase, include the numerics but exclude all the spaces, whitespaces, and other special characters and it reads the same from left to right and right to left.

Answer»
For instance, consider the following sentence: “2 Race, e cAr 2”. This sentence will be converted to “2raceecar2”. The string is a palindrome, hence this sentence is a palindrome. You have to take a sentence input from the user and print “true” if it is a palindrome, or else print “false”.

 A sentence is said to be a palindrome if we convert all its alphabets to lowercase, include the numerics but exclude all the spaces, whitespaces, and other special characters and it reads the same from left to right and right to left.

So, the approach is pretty simple. We convert the sentence into a string by including all the alphanumeric characters and excluding all the other characters. The alphabets will be included only in their lowercase format. Then, we simply have to check whether a string is a palindrome or not. For this, we keep a pointer “lo” at the beginning of the string and a pointer “hi” at the end of the string. We keep incrementing lo and decrementing hi while checking whether the characters at these indices are equal or not. If at any place, we find that the characters are not equal, the string is not a palindrome. If lo becomes greater than hi and the characters at lo and hi were the same throughout, the string is a palindrome. 

Java Code to check Palindromic Sentence

import java.util.*;
class Main {

public static boolean isStrPalindrome(String str) {

int lo = 0;
int hi = str.length()-1;

while(lo < hi) {
char ch1 = str.charAt(lo);
char ch2 = str.charAt(hi);

if(ch1 != ch2) return false;
lo++;
hi--;
}

return true;
}

public static boolean isSentencePalindrome(String s) {
String res = "";

for(int i=0;i<s.length();i++) {
char ch = s.charAt(i);

if((ch >='a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >='0' && ch<='9')) {
if(ch >='A' && ch <= 'Z') res += (char)(ch + 32);
else res += ch;
} else continue;
}

if(isStrPalindrome(res)) return true;
return false;
}

public static void main(String args[]) {
// Your code goes here
Scanner scn = new Scanner(System.in);
String sentence = scn.nextLine();

if(isSentencePalindrome(sentence)) System.out.println(true);
else System.out.println(false);
}

Sample Output

When the sentence is a palindrome

Input: 2 Race, e cAr 2
Output: true

When the sentence is not a palindrome

Input: 2 Race, a cAr 2
Output: false


  • Corner cases, You Might Miss: It is very important to convert all the alphabets in the String to lowercase. If this is not done, our answer will not be correct. Also, the special case of the string being empty is not handled separately as the program automatically covers this test case by not including any character of the string. So, according to our program, an empty string will be a palindrome.

  • If you want that the answer should be false in the case of an empty string, you can apply this condition in the isStrPalindrome() function.


  • Time Complexity: O(N) where N is the length of the input string.
    Auxiliary Space: O(1) as we have not used any extra space.




Discussion

No Comment Found