8. Longest Palindrome in a String
The problem can be found at the following link: Question Link
Problem Description
Given a string s, your task is to find the longest palindromic substring within s.
A substring is a contiguous sequence of characters within the string (i.e.,
s[i...j]for0 ≤ i ≤ j < len(s)).A palindrome reads the same forwards and backwards, i.e.,
reverse(s) == s.If there are multiple palindromic substrings of the same maximum length, return the first occurrence from left to right.
Examples
Example 1:
Input:
s = "forgeeksskeegfor"Output:
"geeksskeeg"Explanation:
Possible palindromic substrings include "kssk", "ss", "eeksskee", etc. However, "geeksskeeg" is the longest among them.
Example 2:
Input:
Output:
Explanation:
The substring "ee" is the longest palindrome in "Geeks".
Example 3:
Input:
Output:
Explanation:
All substrings "a", "b", and "c" have length 1, which is the maximum. Returning the first occurrence results in "a".
Constraints:
$(1 \leq s.\text{size()} \leq 10^3)$
sconsists of only lowercase English letters.
My Approach
Expand Around Center
A palindrome can be expanded from its center.
For each index
iin the string:Consider two scenarios for the center:
Odd-length palindromes (center at
i).Even-length palindromes (center between
iandi+1).
Expand outwards while left and right characters match.
Track the maximum length palindrome found and its start position.
This approach checks each possible center in O(N) and potentially expands in O(N), leading to O(N²) overall time.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(N²), because for each index we can expand in both directions.
Expected Auxiliary Space Complexity: O(1), since we use only a few extra variables for tracking indices and lengths.
Code (C++)
⚡ Alternative Approaches
2️⃣ Dynamic Programming Approach (O(N²) Time, O(N²) Space)
Algorithm Steps:
Create a 2D DP table
dp[i][j], wheredp[i][j]istrueifs[i:j]is a palindrome.Set
dp[i][i] = true(single-character substrings are palindromes).If two adjacent characters are equal (
s[i] == s[i+1]), setdp[i][i+1] = true.For substrings of length 3 or more, use the formula:
dp[i][j] = (s[i] == s[j] && dp[i+1][j-1])
Keep track of the longest palindrome found and return it.
🔹 Easy to understand 🔹 Uses O(N²) space for the DP table
3️⃣ Manacher’s Algorithm (O(N) Time, O(N) Space)
Algorithm Steps:
Transform the original string by inserting special characters (
#) between characters to handle even-length palindromes.Example:
"abc"→"#a#b#c#"
Use a palindrome radius array
p[i]to store the length of the longest palindrome centered ati.Maintain a center (
C) and right boundary (R), representing the rightmost palindrome found.If
iis withinR, mirror the value ofp[i]from the symmetric point acrossC.Expand the palindrome at
iwhile characters match.If the palindrome at
iexpands beyondR, updateCandR.Extract the longest palindrome from
p[i].
🔹 Fastest solution (O(N)) 🔹 Requires string transformation
📊 Comparison of Approaches
Approach
⏱️ Time Complexity
🗂️ Space Complexity
✅ Pros
⚠️ Cons
Expand Around Center
🟡 O(N²)
🟢 O(1)
Simple and uses constant extra space
Slower for larger strings
Dynamic Programming
🟡 O(N²)
🟡 O(N²)
Straight-forward to implement
High space usage (DP table)
Manacher’s Algorithm
🟢 O(N)
🟢 O(N)
Fastest known approach
String transformation can be tricky to code
💡 Best Choice?
✅ For best runtime performance: Use Manacher’s Algorithm (O(N)).
✅ For simplicity and minimal space usage: Use Expand Around Center (O(N²)).
✅ For detailed table-based logic understanding: Use Dynamic Programming (O(N²)).
Code (Java)
Code (Python)
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated