Core Java

Palindrome Java Program

In this example, we are going to see a Palindrome Java Program, to check whether a String is a palindrome. A String is considered a palindrome if it can be similarly read both from left to right and from right to left. For example "abbcbba", “12321”, “69796” are all palindromes.

1. Palindrome Java Program – Simple approach

It’s very easy to think of a simple algorithm that can check if a String is a palindrome. You take the String, let’s say , "abbcbba", and reverse it : "abbcbba". Exactly the same. On the other hand, if you reverse a non-palindrome String, let’s say "java", you came up with "avaj".

So a very effective first approach would be to take the initial String, reverse it and check if the result String is equal to the original. If it is, then our input String is a palindrome, else it’s not.

PalindromeExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.javacodegeeks.core.palindrome;
 
public class PalindromeExample {
 
    private static final String STR1 = "abbcbba";
    private static final String STR2 = "isdovosjd";
 
    public static void main(String[] args) {
         
         
        System.out.println("String :"+STR1+" is a palindrome :"+PalindromeExample.isPalindrome(STR1));
        System.out.println("String :"+STR2+" is a palindrome :"+PalindromeExample.isPalindrome(STR2));
 
    }
 
    public static boolean isPalindrome(String str){
 
        String reverse = new StringBuffer(str).reverse().toString();
         
        if (reverse.equals(str))
            return true;
         
        return false;
    }
}

The output of the program is:

1
2
String :abbcbba is a palindrome :true
String :isdovosjd is a palindrome :false

Of course, there are many ways you can compute the reverse of a given String. and you can find a good guide that lists different approaches, in this example : Java String reverse Example.

2. A more efficient algorithm

It’s not hard to see that in the above example, reversing a String requires double the memory along with some coping, and of course the additional computations of the equals method. Non of the above are particularly costly, but you could think of a more efficient and faster solution.

Our second approach is based on the intuition that a String is a palindrome if the first half of the String is “mirrored” by the other half. This means that the first half of the String read from left to right, is the same as the second half read from right to left.

If you imagine the String as a char array, then it’s straightforward to implement an algorithm that takes advantage of that idea:

  1. Have a pointer pointing to the first element of the array, and a second pointer pointing at the last element of the array.
  2. The first pointer will advance from left to right, and the second one from right to left.
  3. At each step, check if the characters pointed by the two pointers are the same. If they are, move the first pointer one position to the right and the second pointer on position to the left. If the are not equal, then the String is not a palindrome.
  4. If the two pointers meet half-way, then the String is a palindrome.

PalindromeExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.javacodegeeks.core.palindrome;
 
public class PalindromeExample {
 
    private static final String STR1 = "uabbcbbau";
    private static final String STR2 = "isdovosjd";
 
    public static void main(String[] args) {
        System.out.println("String :"+STR1+" is a palindrome :"+PalindromeExample.isPalindrome2(STR1));
        System.out.println("String :"+STR2+" is a palindrome :"+PalindromeExample.isPalindrome2(STR2));
    }
 
    public static boolean isPalindrome(String str){
 
        String reverse = new StringBuffer(str).reverse().toString();
         
        if (reverse.equals(str))
            return true;
         
        return false;
    }
     
    public static boolean isPalindrome2(String str){
 
        int start = 0;
        int end = str.length() - 1;
        int half = end/2;
         
        for(int i = 0; i < half; i++, start++, end-- ){
            if(str.charAt(start) != str.charAt(end))
                return false;
        }
         
        return true;
    }
}

The output of the program is:

1
2
String :uabbcbbau is a palindrome :true
String :isdovosjd is a palindrome :false

It’s fairly simple.

3. Download the Source Code

This was an example on developing a Palindrome Program in Java.

Download
You can download the full source code of this example here: Palindrome Java Program

Last updated on May 4th, 2020

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button