What is a palindrome string?

A palindrome string is a sequence of characters that reads the same forwards as it does backward.

 In other words, if you reverse the characters of a palindrome string, the result will be identical to the original string.

 Palindromes are not limited to just words; they can be phrases, sentences, or any sequence of characters.


Palindrome strings exhibit a unique symmetry that makes them interesting and often used as puzzles or exercises in programming and language studies.

 The concept of palindromes is commonly encountered in various fields, including linguistics, computer science, mathematics, and even in everyday language.


Here are a few examples of palindrome strings:


"radar" - Reads the same forwards and backward.

"level" - Another example of a palindrome.

"A man, a plan, a canal, Panama!" - A more complex palindrome that ignores spaces and punctuation.

Type-1:

Let's create a Java program that checks if a string is a valid palindrome, which means it reads the same forwards and backward, ignoring spaces and punctuation.

 We'll also provide a detailed explanation of how it works.


public class PalindromeChecker
public static void main(String[] args)
String input = "A man, a plan, a canal, Panama!";
if (isPalindrome(input)) {
 System.out.println("The string is a valid palindrome.");
 }
else {
 System.out.println("The string is not a valid palindrome.");
 }
 }
public static boolean isPalindrome(String input)
// Remove all non-alphanumeric characters and convert the input to lowercase
String cleanInput = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); 
// Initialize pointers for the start and end of the cleaned input 
int left = 0;
int right = cleanInput.length() - 1;
// Check if the characters at the pointers are the same while moving towards the center 
while (left < right) { 
if (cleanInput.charAt(left) != cleanInput.charAt(right)) {
return false
// If characters don't match, it's not a palindrome 
 }
 left++; 
 right--;
 }
return true
// All characters match, it's a palindrome }
 }

Explanation:

  1. The program starts with the main method, where we declare and initialize the input variable with the string we want to check for being a valid palindrome. In this example, the input is set to "A man, a plan, a canal, Panama!".
  2. We call the isPalindrome method, passing the input as an argument, to check if the string is a valid palindrome.
  3. In the isPalindrome method, we first clean the input string by removing all non-alphanumeric characters (using regular expression [^a-zA-Z0-9]) and converting it to lowercase. This step ensures that we only consider alphabetic characters and digits when checking for palindromes, ignoring spaces, punctuation, and case sensitivity.
  4. We initialize two pointers, left and right, for the start and end of the cleaned input, respectively. left starts from the beginning of the string (0), and right starts from the end of the string (cleanInput.length() - 1).
  5. We use a while loop to check if the characters at the left and right pointers are the same while moving towards the center of the string. The loop continues until left is less than right, which means we haven't checked all characters yet.
  6. Inside the loop, we compare the characters at the left and right positions in the cleaned input using cleanInput.charAt(left) and cleanInput.charAt(right). If they are not the same, it means the string is not a valid palindrome, and we immediately return false.
  7. If the characters at the left and right positions match, we increment left and decrement right to move towards the center of the string and continue the comparison.
  8. The loop continues until left becomes equal to or greater than right, meaning we have checked all characters or reached the center of the string.
  9. If the loop completes without finding any mismatching characters, it means the string is a valid palindrome, and we return true.


By using this approach, we efficiently check if a given string is a valid palindrome by ignoring spaces, punctuation, and case sensitivity. The method provides a simple and effective way to determine whether a string reads the same forwards and backward.


Type -2:

 Another way to check if a string is a valid palindrome is by reversing the string and comparing it with the original string after removing non-alphanumeric characters and converting both to lowercase. 

If the reversed string matches the cleaned original string, then the input string is a valid palindrome.  Here's a Java program using this approach:


public class PalindromeChecker
public static void main(String[] args) {
String input = "A man, a plan, a canal, Panama!";
if (isPalindrome(input)) {
 System.out.println("The string is a valid palindrome.");
 } else {
 System.out.println("The string is not a valid palindrome.");
 } 
 } 
public static boolean isPalindrome(String input)
// Remove all non-alphanumeric characters and convert the input to lowercase
String cleanInput = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); 
// Reverse the cleaned input string 
String reversedInput = new StringBuilder(cleanInput).reverse().toString();
// Compare the cleaned input with the reversed string 
return cleanInput.equals(reversedInput); }
 }

Explanation:

  1. The code structure and the declaration of the main method are similar to the previous implementation.
  2. We call the isPalindrome method, passing the input as an argument, to check if the string is a valid palindrome.
  3. In the isPalindrome method, we first clean the input string by removing all non-alphanumeric characters (using regular expression [^a-zA-Z0-9]) and converting it to lowercase, just like in the previous implementation.
  4. We then use a StringBuilder to reverse the cleaned input string. The reverse() method of StringBuilder is used to reverse the characters in the string.
  5. After reversing the string, we convert the StringBuilder back to a string using the toString() method and store it in the reversedInput variable.
  6. Finally, we compare the cleaned input (cleanInput) with the reversed string (reversedInput) using the equals() method. If they are the same, it means the input string is a valid palindrome, and we return true. Otherwise, we return false.


By using this approach, we efficiently check if a given string is a valid palindrome by ignoring spaces, punctuation, and case sensitivity. The method provides a concise way to determine whether a string reads the same forwards and backward. It avoids manual comparisons using pointers and loops, making the code more straightforward.

Type-3:

Another way to check if a string is a valid palindrome is by using a two-pointer approach without modifying the original string. 

This approach involves comparing characters from both ends of the string and moving the pointers towards the center until they meet. 

Let's create a Java program using this two-pointer approach:


public class PalindromeChecker {
public static void main(String[] args) {
String input = "A man, a plan, a canal, Panama!";
if (isPalindrome(input)) {
 System.out.println("The string is a valid palindrome.");
 } else {
 System.out.println("The string is not a valid palindrome.");
 }
 }
public static boolean isPalindrome(String input)
// Initialize pointers for the start and end of the input string 
int left = 0;
int right = input.length() - 1
// Continue comparing characters from both ends while moving towards the center
while (left < right) { 
// Move the left pointer to the next alphanumeric character 
while (left < right && !Character.isLetterOrDigit(input.charAt(left))) { 
 left++;
 } 
// Move the right pointer to the next alphanumeric character 
while (left < right && !Character.isLetterOrDigit(input.charAt(right))) { 
 right--; 
 } 
// Compare characters at the left and right pointers
if (Character.toLowerCase(input.charAt(left)) != Character.toLowerCase(input.charAt(right))) { 
return false;
// If characters don't match, it's not a palindrome 
 }
 left++;
// Move the left pointer towards the center 
 right--; 
// Move the right pointer towards the center 
 }
return true
// All characters match, it's a palindrome
}
 }

Explanation:

  1. The code structure and the declaration of the main method are similar to the previous implementations.
  2. We call the isPalindrome method, passing the input as an argument, to check if the string is a valid palindrome.
  3. In the isPalindrome method, we initialize two pointers, left and right, for the start and end of the input string, respectively.
  4. We use a while loop to compare characters from both ends of the input string while moving the pointers towards the center. The loop continues until left is less than right, which means we haven't checked all characters yet.
  5. Inside the loop, we use two nested while loops to move the left and right pointers to the next alphanumeric character from their current positions. We use Character.isLetterOrDigit() to check if a character is alphanumeric.
  6. After moving both pointers to valid alphanumeric characters, we compare the characters at the left and right positions. We use Character.toLowerCase() to ignore case sensitivity while comparing.
  7. If the characters at the left and right positions do not match, it means the string is not a valid palindrome, and we immediately return false.
  8. If the characters at the left and right positions match, we increment left and decrement right to move both pointers towards the center and continue the comparison.
  9. The loop continues until left becomes equal to or greater than right, meaning we have checked all characters or reached the center of the string.
  10. Finally, if the loop completes without finding any mismatching characters, it means the string is a valid palindrome, and we return true.


By using this two-pointer approach, we efficiently check if a given string is a valid palindrome without modifying the input string or using regular expressions.

 The method handles spaces, punctuation, and case sensitivity while determining whether the string reads the same forwards and backward.