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.