How to Check Palindrome in Java
What is a Palindrome?
A palindrome reads the same forwards and backwards! Examples: "racecar", "madam", "12321"
Solutions
Method 1: Using StringBuilder
public static boolean isPalindrome(String str) {
String reversed = new StringBuilder(str).reverse().toString();
return str.equals(reversed);
}
// Usage
System.out.println(isPalindrome("racecar")); // true
System.out.println(isPalindrome("hello")); // falseMethod 2: Using Two Pointers
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;
}
// Usage
System.out.println(isPalindrome("madam")); // trueCase-Insensitive Palindrome Check
public static boolean isPalindrome(String str) {
str = str.toLowerCase().replaceAll("[^a-z0-9]", "");
int left = 0, right = str.length() - 1;
while (left < right) {
if (str.charAt(left++) != str.charAt(right--)) {
return false;
}
}
return true;
}
// Usage
System.out.println(isPalindrome("A man, a plan, a canal: Panama")); // trueDid You Know?
The longest single-word palindrome in English is "tattarrattat" (a knock on the door), coined by James Joyce in Ulysses!
Pro Tip
The two-pointer method is more efficient (O(n/2)) than reversing the string (O(n)) because it can exit early when a mismatch is found!
Real-World Use Cases
- Word games: Validate palindrome words
- Data validation: Check symmetric patterns
- DNA sequences: Identify palindromic sequences
- Algorithms: String manipulation challenges
›Short versionOpen if you only have a minute.
- Two pointers, one at each end, compare characters and move inward.
- Return false on the first mismatch. The center character of an odd length has no pair.
- Building a reversed copy works and spends an extra string. The pointers do not.
- Case and punctuation are part of the contract. "Aa" differs from "aa" until you fold case.
Palindrome check
Check whether text reads the same forward and backward under the comparison you chose.
Working approach
- Start i at 0 and j at length - 1.
- While i < j, compare charAt(i) and charAt(j).
- If the contract ignores case, fold both characters with Character.toLowerCase before comparing.
Walk the sample
- Input "madam"
- m matches m
- a matches a
- The center d has no pair
- It matches
The trap
A reversed-copy check hides an O(n) allocation, and a case-sensitive compare rejects "Aa" even when the reader thinks of it as the same letter.
static boolean isPalindrome(String text) {
int i = 0;
int j = text.length() - 1;
while (i < j) {
if (text.charAt(i) != text.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}Is "Aa" a palindrome under a direct char comparison?
No. 'A' and 'a' are different char values, so the pointers stop on the first pair.
Fold case only when that is the contract. Do not also delete spaces unless the section asked for a phrase palindrome. The empty string passes, because the loop never runs.
Time O(n), extra space O(1) with two pointers.
Easy to get wrong
- Allocating a reversed string when the pointers are enough.
- Ignoring case, or folding it, without matching the comparison the caller asked for.