Check Whether a String Is a Palindrome
Return whether a string reads identically forwards and backwards.
Compare the string from both ends inward: a `left` pointer at the start and a `right` pointer at the end. If any pair of characters differs, it is not a palindrome. If the pointers meet without a mismatch, it is. This runs in O(n) time and O(1) extra space.
Problem Statement
Given a string, determine whether it is a palindrome — that is, whether it reads the same from left to right as from right to left. This version compares the characters exactly as given (case-sensitive, spaces included).
For example, "madam" is a palindrome, while "hello" is not.
Input: A string s.
Output: A boolean: true if s is a palindrome, false otherwise.
Examples
Input: "madam"
Output: truem==m and a==a; the middle d has no partner, so it reads the same both ways.
Input: "hello"
Output: falseThe first character h does not match the last character o.
Constraints
0 <= s.length() <= 10^6Comparison is exact (no case or space normalization)
Think Before You Code
Reveal the questions to ask yourself first
- Which two characters must be equal first for a string to read the same both ways?
- Can you stop as soon as you find a single mismatch?
- Does the middle character of an odd-length string need to be compared to anything?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Two pointers comparing from the outside in.
- Set
left = 0andright = s.length() - 1. - While
left < right:- If
s.charAt(left) != s.charAt(right), return false. - Otherwise increment
leftand decrementright.
- If
- If the loop completes with no mismatch, return true.
You only need to reach the middle; once left meets right, every mirrored pair
has matched.
Dry Run
Walk through the example step by step
Checking "madam":
left | right | s[left] | s[right] | match?
-----+-------+---------+----------+-------
0 | 4 | m | m | yes
1 | 3 | a | a | yes
2 | 2 | d | d | left<right false -> stop
No mismatch found, so "madam" is a palindrome (true).
Solution
Reveal the full Java solution
public class StringPalindrome {
public static boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) {
System.out.println(isPalindrome("madam")); // true
System.out.println(isPalindrome("hello")); // false
}
}
A palindrome is symmetric about its center, so the character at position i from
the start must equal the character at position i from the end. Comparing pairs
from both ends checks exactly that, and a single mismatch is enough to reject the
string early. Reaching the middle with no mismatch proves symmetry — O(n) time, O(1) space.
O(n)Space: O(1)Common Mistakes
- Building a reversed copy and comparing, which works but uses O(n) extra space unnecessarily.
- Using `==` on substrings or characters incorrectly; for chars `==` is fine, but for String objects use `.equals`.
Edge Cases to Test
- An empty string is a palindrome (the loop never runs).
- A single character is a palindrome.
- Case and spaces matter here, so "Madam" is false under exact comparison.
Interview Follow-Ups
- How would you ignore case and spaces to treat a full sentence as a palindrome?
- How would you check the palindrome property recursively?
- How would you find the longest palindromic substring?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →