Check Palindrome String in Java
Problem
A palindrome is a string that reads the same forwards and backwards.
Given a string, determine whether it is a palindrome.
Java Program
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
// Build the reversed version of the string
String reversed = new StringBuilder(str).reverse().toString();
// A palindrome reads the same forwards and backwards
boolean isPalindrome = str.equals(reversed);
System.out.println(str + " is palindrome: " + isPalindrome);
}
}Output
Core Logic
The simplest check is to reverse the string and see if it matches the original — StringBuilder makes that a one-liner.
- 1The original string is passed to
new StringBuilder(str), which wraps it in a mutable buffer. - 2
.reverse()flips the character order in place, and.toString()converts the buffer back into a String. - 3
str.equals(reversed)compares the two strings character by character, not by reference. - 4If every character matches its mirrored position,
equals()returnstrueand the string is confirmed a palindrome.
"madam", reversing it also produces "madam", so equals() returns true.Key Point: Always compare with .equals(), not == — == checks object reference, not character content, and would give the wrong answer here.
Key Concepts
Approach 2: Manual Two-Pointer Comparison
public class PalindromeCheckManual {
public static void main(String[] args) {
String str = "madam";
boolean isPalindrome = true;
int left = 0, right = str.length() - 1;
// Move left and right toward each other, comparing mirrored characters
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
isPalindrome = false;
break; // mismatch found, no need to keep checking
}
left++;
right--;
}
System.out.println(str + " is palindrome: " + isPalindrome);
}
}
Output
Core Logic
No built-in reverse this time — just two pointers closing in from opposite ends, comparing characters as they go.
- 1Two index variables,
leftandright, start at the first and last character positions. - 2On each iteration,
charAt(left)andcharAt(right)are compared — if they differ, the string cannot be a palindrome, soisPalindromeis set tofalseand the loop exits early withbreak. - 3If the characters match,
leftmoves inward andrightmoves inward, narrowing the comparison window. - 4The loop stops once
leftmeets or crossesright, meaning every pair has been checked.
"madam", the pairs checked are (m,m) and (a,a); the loop stops once left and right meet at the middle 'd' — no mismatch was found, so isPalindrome stays true.Key Point: This runs in the same O(n) time as the StringBuilder version, but uses O(1) extra space instead of O(n), since it never builds a second, reversed copy of the string — a detail interviewers often ask about directly.
Key Concepts
Approach 3: Recursive Check
public class PalindromeCheckRecursive {
static boolean isPalindrome(String str, int left, int right) {
// Base case: pointers met or crossed, every pair matched
if (left >= right) return true;
// Mismatch found, not a palindrome
if (str.charAt(left) != str.charAt(right)) return false;
// Recurse inward, shrinking the comparison window
return isPalindrome(str, left + 1, right - 1);
}
public static void main(String[] args) {
String str = "madam";
boolean isPalindrome = isPalindrome(str, 0, str.length() - 1);
System.out.println(str + " is palindrome: " + isPalindrome);
}
}
Output
Core Logic
The same two-pointer idea also works recursively: each call checks one pair of characters and hands off a slightly smaller window to the next call.
- 1The base case
if (left >= right) return true;fires once the pointers meet or cross, meaning every pair has matched. - 2Each call compares
str.charAt(left)andstr.charAt(right)— a mismatch immediately returnsfalse, short-circuiting the recursion. - 3If the characters match, the call recurses inward with
isPalindrome(str, left + 1, right - 1). - 4The recursion naturally stops as soon as either a mismatch is found or the pointers meet.
"madam", the calls compare (m,m) then (a,a), then hit the base case at the middle 'd', returning true all the way back up.Key Point: Functionally identical to the iterative two-pointer version, but each recursive call adds a stack frame — for very long strings, the loop-based version avoids that overhead.