C Program
#include <stdio.h> #include <string.h> int expand(char s[], int left, int right) { int n = strlen(s); while (left >= 0 && right < n && s[left] == s[right]) { left--; right++; } return right - left - 1; } int main() { char s[100]; scanf("%s", s); int start = 0, maxLen = 0, n = strlen(s); for (int i = 0; i < n; i++) { int len1 = expand(s, i, i); int len2 = expand(s, i, i+1); int len = len1 > len2 ? len1 : len2; if (len > maxLen) { maxLen = len; start = i - (len - 1) / 2; } } for (int i = start; i < start + maxLen; i++) printf("%c", s[i]); return 0; }
C Output
Input:
babad
Output:
bab
C++ Program
#include <bits/stdc++.h> using namespace std; int expand(string s, int left, int right) { while (left >= 0 && right < s.size() && s[left] == s[right]) { left--; right++; } return right - left - 1; } int main() { string s; cin >> s; int start = 0, maxLen = 0; for (int i = 0; i < s.size(); i++) { int len1 = expand(s, i, i); int len2 = expand(s, i, i+1); int len = max(len1, len2); if (len > maxLen) { maxLen = len; start = i - (len - 1) / 2; } } cout << s.substr(start, maxLen); }
C++ Output
Input:
cbbd
Output:
bb
JAVA Program
import java.util.Scanner; public class Main { public static int expand(String s, int left, int right) { while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { left--; right++; } return right - left - 1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); int start = 0, maxLen = 0; for (int i = 0; i < s.length(); i++) { int len1 = expand(s, i, i); int len2 = expand(s, i, i+1); int len = Math.max(len1, len2); if (len > maxLen) { maxLen = len; start = i - (len - 1) / 2; } } System.out.println(s.substring(start, start + maxLen)); } }
JAVA Output
Input:
forgeeksskeegfor
Output:
geeksskeeg
Python Program
def expand(s, left, right): while left >= 0 and right < len(s) and s[left] == s[right]: left -= 1 right += 1 return right - left - 1 s = input().strip() start, max_len = 0, 0 for i in range(len(s)): len1 = expand(s, i, i) len2 = expand(s, i, i+1) length = max(len1, len2) if length > max_len: max_len = length start = i - (length - 1) // 2 print(s[start:start+max_len])
Python Output
Input:
abacdfgdcaba
Output:
aba
Deep Explanation
Example
Consider the string "babad".
Bouncing from each character, we move outwards to look for palindromes.
Beginning at index 0 ("b"), the palindrome is "bab".
At index 2 ("a"), the palindrome is "aba".
Both are of length 3, so the answer might be "bab" or "aba".
Real-Life Analogy
Imagine a palindrome as a mirror reflection. Stand at the center of a bridge spanning a river. If the view on the left is exactly the same as the view on the right, you have perfect symmetry—a reflection of what occurs as we work our way outward from a center and see if the characters are the same. If symmetry is lost, the mirror effect is broken, and the palindrome is over.
Why It Matters
Identifying the longest palindromic substring is not only a programming brain teaser, it's a significant string manipulation problem that comes up in interviews and competitive programming. It challenges your skill at dealing with substrings, searching efficiently, and considering symmetry in data structures. It also finds uses in DNA sequencing (where genome patterns may be palindromic), text analysis, and error detection algorithms.
Learning Insights
The brute-force method would be to test all substrings and determine if they're palindromes, which is O(n³) time. The "expand around center" optimization cuts this down to O(n²), and that's much faster and common in interviews. There's even a more complex algorithm called Manacher's Algorithm that does this in O(n), but it's overkill. For studying purposes, the center expansion method gets the balance between simplicity and speed just right.
Interview Relevance
This question is a favorite among interviews in organizations such as Amazon, Google, and Microsoft due to the fact that it not only tests string manipulation but also optimization strategies. The interviewers would want you to start by explaining brute-force, then describe improving step by step until you end with the center-expansion approach. They might even go as far as asking you to mention Manacher's Algorithm if you are a seasoned candidate.
Practical Applications
Palindromes are applied in pattern recognition, cryptography, data compression, and even palindrome-based DNA structures in bioinformatics. For example, some DNA sequences are the same when read forwards and reversed, and finding the longest palindromic substring assists in genetic study.
SEO-Optimized Closing
Longest Palindromic Substring is one of the most trending coding problems for freshers and interview practice. Learning how to grow around the center, why symmetry with palindromes is important, and how to apply optimal solutions in C, C++, Java, and Python assists students in solidifying their string-handling abilities. This programming challenge is not only a great practice for learning substrings but also a door to more complex string algorithms. If you’re preparing for coding interviews or competitive exams, practicing longest palindromic substring solutions will greatly improve your problem-solving and optimization skills.
Social Plugin