VOOZH about

URL: https://www.geeksforgeeks.org/dsa/longest-palindromic-substring/

⇱ Longest Palindromic Substring - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Longest Palindromic Substring

Last Updated : 3 Oct, 2025

Given a string s, find the longest substring which is a palindrome. If there are multiple answers, then find the first appearing substring.

Examples:

Input: s = "forgeeksskeegfor"
Output: "geeksskeeg"
Explanation: The longest substring that reads the same forward and backward is "geeksskeeg". Other palindromes like "kssk" or "eeksskee" are shorter.

Input: s = "Geeks"
Output: "ee"
Explanation: The substring "ee" is the longest palindromic part in "Geeks". All others are shorter single characters.

Input: s = "abc"
Output: "a"
Explanation: No multi-letter palindromes exist. So the first character "a" is returned as the longest palindromic substring.

[Naive Approach] Generating all sub-strings - O(n3) time and O(1) space

Generate all possible substrings of the given string. For each substring, check if it is a palindrome.
If it is, update the result if its length is greater than the longest palindrome found so far.


Output
geeksskeeg

[Better Approach - 1] Using Dynamic Programming - O(n2) time and O(n2) space

The idea is to use Dynamic Programming to store the status of smaller substrings and use these results to check if a longer substring forms a palindrome.

  • The main idea behind the approach is that if we know the status (i.e., palindrome or not) of the substring ranging [i, j], we can find the status of the substring ranging [i-1, j+1] by only matching the character str[i-1] and str[j+1].
  • If the substring from i to j is not a palindrome, then the substring from i-1 to j+1 will also not be a palindrome. Otherwise, it will be a palindrome only if str[i-1] and str[j+1] are the same.
  • Base on this fact, we can create a 2D table (say table[][] which stores status of substring str[i . . . j] ), and check for substrings with length from 1 to n. For each length find all the substrings starting from each character i and find if it is a palindrome or not using the above idea. The longest length for which a palindrome formed will be the required answer.

Note: Refer to Longest Palindromic Substring using Dynamic Programming for detailed approach.


Output
geeksskeeg

[Better Approach - 2] Using Expansion from center - O(n2) time and O(1) space

The idea is to traverse each character in the string and treat it as a potential center of a palindrome, trying to expand around it in both directions while checking if the expanded substring remains a palindrome.
=> For each position, we check for both odd-length palindromes (where the current character is the center) and even-length palindromes (where the current character and the next character together form the center).
=> As we expand outward from each center, we keep track of the start position and length of the longest palindrome found so far, updating these values whenever we find a longer valid palindrome.

Step-by-step approach:

  • Iterate through each character in the string, treating it as the center of a potential palindrome.
  • For each center, expand in two ways: one for odd-length palindromes (center at index i) and one for even-length palindromes (center between indices i and i+1)
  • Use two pointers low and high to track the left and right boundaries of the current palindrome.
  • While low and high are in bounds and s[low] == s[high], expand outward.
  • If the current palindrome length (high - low + 1) is greater than the previous maximum, update the starting index and max length.
  • After checking all centers, return the substring starting at start with length maxLen.

Output
geeksskeeg

[Expected Approach] Using Manacher’s Algorithm - O(n) time and O(n) space

The idea is to use Manacher’s algorithm, which transforms the input string by inserting separators (#) and sentinels to handle both even and odd-length palindromes uniformly.
For each position in the transformed string, we expand the longest possible palindrome centered there using mirror symmetry and previously computed values.
This expansion is bounded efficiently using the rightmost known palindrome range to avoid redundant checks.
Whenever a longer palindrome is found, we update its length and starting index in the original string.


Output
geeksskeeg
Comment