This problem is about checking whether a word reads the same from left to right and from right to left. A word like that is called a palindrome.
Your answer is only true or false. If the letters match when you compare the front and the back, the word is a palindrome. If they stop matching at any point, the answer becomes false. You do not need to rearrange anything or count anything. You only need to compare the string with its backward version.
For example, "madam" is true because it looks the same both ways. "racecar" is also true. But "hello" is false because the letters do not match when you read it backwards. An empty string counts as true because nothing breaks the mirror pattern.
The heart of the problem is simple: does the string stay the same when reversed? Once you answer that clearly, the final yes-or-no result becomes easy to decide.
Example Input & Output
Example 1: "madam" reads the same forwards and backwards
Example 2: "hello" does not read the same backwards
Example 3: "racecar" is a palindrome
Algorithm Flow

Solution Approach
A straightforward way to solve this problem is to compare the string with its reversed form. If both versions are exactly the same, then the string is a palindrome. If they differ, then it is not.
This works because the definition of a palindrome is directly about symmetry. The string must read the same from left to right and from right to left. So creating a reversed version and checking equality gives a very direct test of the definition.
In JavaScript, a short version looks like this:
The call to split('') turns the string into an array of characters. Then reverse() flips that array, and join('') rebuilds the reversed string. If that reversed string matches the original, the answer is true.
Another equally valid approach is the two-pointer method. Start one pointer at the beginning of the string and another at the end. Compare the characters at those positions. If they ever differ, return false. If the pointers cross without a mismatch, return true. That version avoids building a reversed copy and still follows the same palindrome idea.
For example, with "madam", the first and last characters both match, the next inner pair also matches, and the center character does not cause any problem, so the word is a palindrome. With "hello", the first character h and the last character o already differ, so the answer is false. An empty string returns true because there is nothing that breaks the mirror pattern.
The reverse-and-compare approach runs in O(n) time because each character is processed a constant number of times. The extra space is O(n) for the reversed copy. The two-pointer approach also runs in O(n) time but can use O(1) extra space.
So the full strategy is: either reverse the string and compare it to the original, or compare mirrored characters from both ends. If every mirrored position matches, return true; otherwise return false.
Best Answers
class Solution {
public boolean is_palindrome(String s) {
String rev = new StringBuilder(s).reverse().toString();
return s.equals(rev);
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
