Write a Java Program to Find the Longest Palindrome Present in a Given String

Write a Java Program to Find the Longest Palindrome Present in a Given String thumbnail
22K
By Dhiraj Ray 10 March, 2018

Program Description

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam. Write a java program to find the longest palindrome present in a given string. For example, in the string abcba, the longest palindrome is abcba and similarly in abcmadamcbamadam, the longest palindrome is abcmadamcba.

package com.devglan.set2;

public class LongestPalindrome {

    public String findTheLongestPalindrome(String str){
        if (str == null) {
            return null;
        }
        String longestPalindrome = String.valueOf(str.charAt(0));
        for (int i = 0; i < str.length() - 1; i++) {
            String returnedPalindrome = findLongestPalindromeWithSpecifiedParameter(str, i, i);
            if (returnedPalindrome.length() > longestPalindrome.length()) {
                longestPalindrome = returnedPalindrome;
            }
            returnedPalindrome = findLongestPalindromeWithSpecifiedParameter(str, i, i + 1);
            if (returnedPalindrome.length() > longestPalindrome.length()) {
                longestPalindrome = returnedPalindrome;
            }
        }
        return longestPalindrome;
    }

    public String findLongestPalindromeWithSpecifiedParameter(String str, int left, int right) {
        if (left > right)
            return null;

        while (left >= 0 && right < str.length() && str.charAt(left) == str.charAt(right)) {
            left--;
            right++;
        }
        return str.substring(left + 1, right);
    }

    public static void main(String[] args){
        LongestPalindrome longestPalindrome = new LongestPalindrome();
        System.out.println("Longest palindrome in abcmadamcbamadam is " + longestPalindrome.findTheLongestPalindrome("abcmadamcbamadam"));
        System.out.println("Longest palindrome in abcba is " + longestPalindrome.findTheLongestPalindrome("abcba"));
    }
}

Explanation

Any given word can be either of even or odd length. For an even length String to be palindrome there can be two midpoints. Hence, we are calling the method findLongestPalindromeWithSpecifiedParameter() twice to handle this condition with left as i and right as i+1.

We are checking for palindrome around each character as the palindrome substring can be at any position in the String and continue the while loop until we find any unequal character.

Share

If You Appreciate This, You Can Consider:

We are thankful for your never ending support.

About The Author

author-image
A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Technical expertise in highly scalable distributed systems, self-healing systems, and service-oriented architecture. Technical Skills: Java/J2EE, Spring, Hibernate, Reactive Programming, Microservices, Hystrix, Rest APIs, Java 8, Kafka, Kibana, Elasticsearch, etc.