πDay 5. 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.
π Example Walkthrough:
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.
π Solution Code
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