logo
  • Programming
  • Testing
  • AI
  • Devops
  • Data Science
  • Design
  • Blogs
  • Crypto Tools
  • Dev Feed
  • Login
Story
Follow @devglan

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

author-image   By Dhiraj Ray,   10 March, 2018 20K

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

Click to Suggest Your Own 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.

Other Similar Java Programs:

1 Java Program to test if given number is Armstrong or not
2 Java Program to test if a given number is Fibonacci or not
3 java program to find distinct word list from a file
4 Java program to find duplicate character from a string
5 Java Program to find middle index of array where both ends sum is equal
6 Java Program to find line with max character length in descending order in Java
7 Java Program to find max two numbers in an array
8 Java program to find max repeated words from a file
9 Java program to find sum of prime numbers
10 Java program to reverse a given number
11 Java program to find permutations of a given string
12 Java program to reverse a given string
13 Java program to find factorial of a given number
14 Java Program for Binary Search
15 Java Program to Add Two 2D Matrix
16 3 Ways to Check if Given Words are Anagram or not
17 Java Program to Find LCM of a Two Given Number
18 Check Given String is Rotation of Another String
19 Java Program To Check If A Given Number is A Perfect Number
20 Remove Common Characters From Given Strings
21 Find Second Largest Number in Array
22 Java Program to Reverse an Array in Place Without Using Any Second Array
23 Java Program to Print 1 To 10 Without Using Loop
24 Write a Java Program to Compare Files in Java
25 Java Program to Find missing Number in an Array
26 Java Program to Find First non Repeated Character in a String
27 Write a Java Program to Find Union and Intersection of Arrays in Java
28 Writing a Java program to rotate an array by d elements.
29 Write a Java program to rotate a matrix
30 Write a Java program to find the largest sum of the contiguous subarray in a given Array

If You Appreciate This, You Can Consider:

  • Share this article on social media or with your teammates.

Suggest Explanation

The suggestion has been saved for review.Thanks for your effort.

{{errorMessage}}

Vertex

Devglan is one stop platform for all
programming tutorials and courses.

About Us

  • About Us
  • Contact Us
  • Submission Criteria
  • Privacy Policy

Quick Links

  • Home
  • Login / Join
  • Submit Your Story
  • Donate

Contact Us

Dhiraj
dhiraj@devglan.com

© 2021 Devglan. All rights reserved.