Find the Longest Palindromic Substring in Java

In this tutorial, we’ll learn how to find the longest palindromic substring in a given string using Java. This is a famous interview question asked by companies like Google, Amazon, and Microsoft.

๐Ÿง  What is a Palindromic Substring?

A palindrome is a string that reads the same backward as forward. A palindromic substring is a part of the string that is also a palindrome.

๐Ÿ“Œ Example

Input: "babad"
Output: "bab" or "aba"

Input: "cbbd"
Output: "bb"

✅ Java Program (Expand Around Center Approach)


public class LongestPalindrome {
    public static void main(String[] args) {
        String input = "babad";
        String result = longestPalindrome(input);
        System.out.println("Longest Palindromic Substring: " + result);
    }

    static String longestPalindrome(String s) {
        if (s == null || s.length() < 1) return "";

        int start = 0, end = 0;

        for (int i = 0; i < s.length(); i++) {
            int len1 = expandFromMiddle(s, i, i);       // Odd length
            int len2 = expandFromMiddle(s, i, i + 1);   // Even length
            int len = Math.max(len1, len2);

            if (len > end - start) {
                start = i - (len - 1) / 2;
                end = i + len / 2;
            }
        }

        return s.substring(start, end + 1);
    }

    static int expandFromMiddle(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }
        return right - left - 1;
    }
}

✅ Output

Longest Palindromic Substring: bab

๐Ÿ’ก Interview Tip

This method runs in O(n^2) time and uses O(1) space. You might also be asked to implement it using dynamic programming, which uses extra space but follows a tabulation-based approach.

๐Ÿ“š Conclusion

We explored how to find the longest palindromic substring using the expand-around-center technique in Java. This problem is a favorite in coding interviews and helps sharpen your understanding of string manipulation and two-pointer techniques.

๐Ÿ”„ Master Palindrome & Advanced String Problems

Finding the longest palindromic substring is a classic problem that tests string traversal, optimization techniques, and algorithmic thinking. Strengthen your Java string problem-solving skills by exploring these related problems and interview resources.

๐Ÿง  Java Coding Round Questions

Practice advanced string and algorithmic coding problems.

๐Ÿ” Palindrome String Program

Start with basic palindrome checks before solving substring variants.

๐Ÿ” First Non-Repeated Character

Practice frequency-based string traversal logic.

๐Ÿงฎ Check Anagram Strings

Learn character frequency comparison techniques.

๐Ÿ” Reverse String in Java

Understand string reversal — a building block for palindrome problems.

๐Ÿ”ข String Contains Only Digits

Practice string validation and traversal techniques.

๐Ÿ“ˆ Character Occurrence Count

Learn frequency counting used in many string algorithms.

๐Ÿ“ฆ Java Collections Interview Questions

Understand data structures used in string optimization problems.

๐ŸŽฏ Java Interview Questions (Freshers)

Common interview questions involving string manipulation.

๐Ÿงช Advanced Java Programs (Real-World)

Apply advanced string algorithms in real-world Java scenarios.