Site Color

Text Color

Ad Color

Text Color





Sign Up to Save Your Colors


Is String A ? by@harrison_brock

Is String A ?

Harrison Brock Hacker Noon profile picture

Harrison Brock

You are given a string and are asked to write a method to return true if the string is a palindrome and to return false otherwise. A palindrome is a string that reads the same from front to back like alula

They are many solutions to check if a string is a palindrome. I’m going to show you three solutions. The first will use a StringBuilder and the second will use a Character Array to solve the problem. For the third solution, we will solve the problem in-place.

Solution Using StringBuilder

In this solution we:

  1. Create a StringBuilder on line 3.
  2. Reverse the StringBuilder and create a new String on line 5.
  3. Use the equals method to see two strings are the same on line 6.

Solution Using CharArray

In this solution we:

  1. Create a new character array on line 4 using the toCharArray() method.
  2. Loop through the array on line 6.
  3. Check if the two values are not the same on line 8. If the values are not the same we return false.
  4. Return true on line 10.

Solution In-Place

This solution is almost the same as the last one but, we use the charAt method to compare the characters. This solution is faster because it loops over the array at most once and does not use more memory.


You have seen different ways to check if a string is a Palindrome or not in this post. If you are asked this question in a technical interview, you will have some solutions under your belt.

Originally published at on January 23, 2019.