Best and Worst Way of Solving the Palindrome Algorithm Question

palindrome algorithm

 Such is the case with solving questions related to the Palindrome Algorithm.  So there are basically 4 different ways you can tackle this algorithm and when we say there’s a best and the worst way we are referring to reducing the space and time complexity.  Today we will be discussing both in the context of the String palindrome in Java programming language. 

Apart from that, we will also discuss the process of implementation of hashmap in Java in this blog. 

Palindrome Algorithm

You have been provided with a string question. The problem statement is that the string series is not empty. You will have to figure out using the string palindrome algorithm whether the words spelled in the given string read similar forwards and backward. 

Our take on the question:

As we have mentioned, we will be using JavaScript to figure out whether the statement we have at hand is true or false. This is the general nature of the programming language for solving problem statements of this sort. 

For beginners who are not accustomed to the whole concept of Java yet, we should mention that the input statement of the problem should not be suggestive. What this means is that the question should not point towards a direct answer else, when the program is run the output will always turn out to be false.  

This will lead to the declaration that the given problem statement is false every time you run the program using Java. 

So what are the best and the worst ways to solve this question you may ask? Well, it’s better if we start by explaining the worst approach. 

Worst approach

Creating a new reversed string

If you were opting for the method of creating a reversed string for the given problem let us stop you right there. 

This approach will have absolutely little to zero effect in redacting the space and time complexity that is required for solving a Palindrome algorithm question. 

In this method, you would essentially create your own reversed string for the letters or the words and try to go through each letter one after the other in a  reversed order to clarify if the word reads the same backward or not. 

Potential problem

The major issue with this approach lies in the time complexity. This approach possesses a big O-notation for the time constraint in the problem. As for the space complexity, we would say that this approach really does it justice. 

That does not go to say that we will be ignoring the other constraint as it is. This is where the approach lags in terms of solving the Palindrome algorithm. 

Once we discuss the other approach it will be pretty clear why we are considering this approach to be the worst possible one. 

Best approach

Appointing the pointer algorithm to compare the furthest ends of the word/words

This is potentially the best approach that you can employ while solving the string palindrome algorithm. All you have to do is use the pointers at both ends of the problem and compare the two letters. 

Next, you can use the while loop and compare the letters associated with the two pointers in the end. If at any given point you consider that the pointers are not indicating similar letters at both ends of the word then the problem statement remains false. 

On the other hand, if you run the program and discover that the two pointers are indicating the same letters at each end of the word, then the given problem statement stands true. 

Why is this approach better?

Well, you can certainly see for yourself that the time and space constraint in the second approach has been drastically improved using the second approach that we have mentioned above. 

This is basically because in this approach you are not required to create an entire reversed string for the given problem statement. 

In the second approach, the pointers make the task of deciding whether the given word is a palindrome or not way easier compared to the first approach. 

Also, the time constraint in deciding whether the statement is true or false has been reduced by a huge margin in the second approach. 

You are requested to keep this in mind if you encounter a problem related to palindrome algorithms in your tech interviews. 

Now, moving on to the next segment of our blog we will be briefly discussing how you can implement a hashmap in Java. 

Implementation of hashmap in Java

The process of implementation of hashmap in Java is known as Hashing. You can implement the process of hashCode() and equals() for implementing the hashmap in Java. 

Here are some of the terms you need to familiarize yourself with in order to use this approach:

  • equals(): The equality aspect of two objects given in a hashmap is checked using this syntax. 
  • hashCode(): This is an approach of the object class. It basically converts the memory reference of the targeted object into the form of an integer. 
  • Buckets: These are also known as the array of the same node. 

If we take a look at the structure of a hashmap we will find that it consists of arrays and nodes. Within the framework of hashing an observer is expected to convert the objects into an integer value. 

This helps in finding out the indexing figure of the given structure. It is also used for faster navigation through the whole system. 

A hashmap usually stores the data in key and value pairs. It also incorporates the linked lists to store the arrays in its structure. 


When it comes to testing the experience of a tech applicant in manipulating a string, there is no better approach than giving them a palindrome question to solve. We hope that this blog has done justice to explaining string palindromes in Java and what approach you can take for the implementation of hashmap in Java.


Leave a Reply