C Program
#include<stdio.h> #include<string.h> int main() { char s[100]; gets(s); int n=strlen(s), l=0, max=1; for(int i=0; i<n; i++) for(int j=i; j<n; j++) { int ok=1; for(int k=0; k<(j-i+1)/2; k++) if(s[i+k] != s[j-k]) { ok=0; break; } if(ok && j-i+1 > max) l=i, max=j-i+1; } for(int i=0; i<max; i++) printf("%c", s[l+i]); }
C Output
Input: babad Output: bab
C++ Program
#include<iostream> using namespace std; int main() { string s; getline(cin, s); int n=s.size(), start=0, len=1; for(int i=0; i<n; i++) { for(int j=0; i-j>=0 && i+j<n && s[i-j]==s[i+j]; j++) if(2*j+1 > len) len=2*j+1, start=i-j; for(int j=0; i-j>=0 && i+j+1<n && s[i-j]==s[i+j+1]; j++) if(2*j+2 > len) len=2*j+2, start=i-j; } cout << s.substr(start, len); }
C++ Output
Input: cbbd Output: bb
JAVA Program
import java.util.*; class P { public static void main(String[] a) { String s = new Scanner(System.in).nextLine(), res = ""; for(int i=0; i<s.length(); i++) { String o = expand(s, i, i), e = expand(s, i, i+1); res = o.length() > res.length() ? o : res; res = e.length() > res.length() ? e : res; } System.out.print(res); } static String expand(String s, int l, int r) { while(l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) l--; r++; return s.substring(l+1, r); } }
JAVA Output
Input: forgeeksskeegfor Output: geeksskeeg
Python Program
s = input() res = '' for i in range(len(s)): for a,b in [(i,i),(i,i+1)]: while a>=0 and b<len(s) and s[a]==s[b]: a-=1; b+=1 if b-a-1 > len(res): res = s[a+1:b] print(res)
Python Output
Input: banana Output: anana
In-Depth Learning – Complete Concept in Paragraphs
What Is the Longest Palindromic Substring?
A palindromic substring is a substring that reads the same way forward and backward. The longest palindromic substring is the longest such string that appears continuously in the string. For instance, in "babad", both "bab" and "aba" are palindromic substrings, and both contain 3 characters.
This exercise teaches you string traversal, pattern identification, and center expansion, a standard and efficient strategy for such problems.
How the Code Works (Center Expansion Logic)
We iterate over every character of the string, considering it to be the center of a possible palindrome, and attempt expanding both:
Around one character (odd-length palindromes)
Around two consecutive characters (even-length palindromes)
At every step:
If we discover a longer palindrome, we update the answer.
Expansion is halted when characters on either side don't match or limits are reached.
This approach prevents brute-force verification of all possible substrings and enhances performance.
Example
Input: "abaxyzzyxf"
Palindromes found:
"aba" → length 3
"xyzzyx" → length 6 (longest)
Output:
nginx
xyzzyx
Real-Life Analogy
Imagine palindromes in terms of symmetry in reflections. If you're standing on a symmetric spot (such as a bridge or building center), you view the identical structure on both sides. Center-expansion reasoning replicates this — it expands the structure similarly to the left and right sides until the symmetry is lost.
Another instance is unfolding a string from the middle and checking if both halves are equal — just like unfolding a letter to a perfect "V" shape.
Where and When Is It Used?
This problem is trending in:
Coding interviews (Amazon, Google, Microsoft, etc.)
Natural language processing (symmetry pattern recognition)
DNA sequence analysis
Data cleaning and symmetry verification
Competitive programming contests
It hones your knowledge of two-pointer algorithms and pattern recognition.
Time and Space Complexity
Method Time Complexity Space Complexity
Brute Force O(n³) O(1)
Center Expansion O(n²) O(1)
Dynamic Programming O(n²) O(n²)
Manacher's Algo O(n) O(n)
Center-expansion is the interview's sweet spot — fast, simple to code, and efficient enough.
Python Optimization Tip
For extra performance:
python
from functools import lru_cache
can be applied to memoize recursive solutions, although center-expansion remains neater for short to medium inputs.
SEO-Optimized Natural Paragraph for Ranking
Searching for the longest palindromic substring solution in C, C++, Java, or Python? This tutorial gives the most concise and elegant implementations with the center expansion method, which effectively gets the largest palindrome in a string. To learn how to identify palindromes with the two-pointer technique is to learn how to master string manipulation, dynamic programming, and interview problems. You are a student, programmer, or interview candidate, this pattern is a good foundation in solving more complex string and sequence problems. Real-life examples and easy-to-understand descriptions make it a perfect guide to learn from.
Social Plugin