A palindrome is a string that reads the same forward and backward. Examples include "madam"
, "level"
, and "racecar"
.
In this article, we’ll explore multiple approaches in Java to determine if a given string is a palindrome. Each method will consider edge cases like case sensitivity and special characters.
What is a Palindrome?
A string is a palindrome if:
- The first character is the same as the last,
- The second is the same as the second last,
- … and so on.
Examples:
Input | Palindrome? |
---|---|
“madam” | ✅ Yes |
“hello” | ❌ No |
“Level” | ✅ Yes (case-insensitive) |
“Was it a car or a cat I saw?” | ✅ Yes (if ignoring spaces/punctuation) |
Approach 1: Reverse the String and Compare
Reverse the string and compare it with the original.
// Java Program to Check the Given String // is Palindrome or not public class PalindromeCheck { public static void main(String[] args) { String str = "madam"; String reversed = new StringBuilder(str).reverse().toString(); if (str.equals(reversed)) { System.out.println("The string is a palindrome."); } else { System.out.println("The string is not a palindrome."); } } }
Approach 2: Character-by-Character Comparison
Use two pointers (start and end) and compare characters one by one.
// Java Program to Check the Given String // is Palindrome or not public class PalindromeCheck { public static boolean isPalindrome(String str) { int left = 0; int right = str.length() - 1; while (left < right) { if (str.charAt(left) != str.charAt(right)) { return false; } left++; right--; } return true; } public static void main(String[] args) { String str = "level"; System.out.println("Is palindrome: " + isPalindrome(str)); } }
Approach 3: Case-Insensitive Check
Convert the string to lowercase to ignore case.
// Java Program to Check the Given String // is Palindrome or not public class PalindromeCheck { public static boolean isPalindrome(String str) { str = str.toLowerCase(); // or str.toUpperCase() int left = 0, right = str.length() - 1; while (left < right) { if (str.charAt(left++) != str.charAt(right--)) { return false; } } return true; } public static void main(String[] args) { String str = "Level"; System.out.println("Is palindrome: " + isPalindrome(str)); } }
Approach 4: Ignoring Non-Alphanumeric Characters
Normalize the string by removing spaces and punctuation.
// Java Program to Check the Given String // is Palindrome or not public class PalindromeCheck { public static boolean isPalindrome(String str) { str = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); int left = 0, right = str.length() - 1; while (left < right) { if (str.charAt(left++) != str.charAt(right--)) { return false; } } return true; } public static void main(String[] args) { String str = "A man, a plan, a canal, Panama!"; System.out.println("Is palindrome: " + isPalindrome(str)); } }
Approach 5: Recursive Solution
Use recursion to compare the first and last characters.
// Java Program to Check the Given String // is Palindrome or not public class PalindromeCheck { public static boolean isPalindrome(String str, int left, int right) { if (left >= right) return true; if (str.charAt(left) != str.charAt(right)) return false; return isPalindrome(str, left + 1, right - 1); } public static void main(String[] args) { String str = "radar"; boolean result = isPalindrome(str, 0, str.length() - 1); System.out.println("Is palindrome: " + result); } }