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:
s = "Geeks"
Output:
"ee"
Explanation:
The substring "ee" is the longest palindrome in "Geeks"
.
Example 3:
Input:
s = "abc"
Output:
"a"
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)$
s
consists of only lowercase English letters.
My Approach
Expand Around Center
A palindrome can be expanded from its center.
For each index
i
in the string:Consider two scenarios for the center:
Odd-length palindromes (center at
i
).Even-length palindromes (center between
i
andi+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++)
class Solution {
public:
string longestPalindrome(string &s) {
int n = s.size(), start = 0, maxLen = 0;
for (int i = 0; i < n; i++) {
for (int l : {i, i + 1}) {
int j = i;
while (j >= 0 && l < n && s[j] == s[l]) j--, l++;
if (l - j - 1 > maxLen) start = j + 1, maxLen = l - j - 1;
}
}
return s.substr(start, maxLen);
}
};
Code (Java)
class Solution {
static String longestPalindrome(String s) {
int n = s.length(), start = 0, maxLen = 0;
for (int i = 0; i < n; i++)
for (int l : new int[]{i, i + 1}) {
int j = i;
while (j >= 0 && l < n && s.charAt(j) == s.charAt(l)) {
j--;
l++;
}
if (l - j - 1 > maxLen) {
start = j + 1;
maxLen = l - j - 1;
}
}
return s.substring(start, start + maxLen);
}
}
Code (Python)
class Solution:
def longestPalindrome(self, s):
start, max_len = 0, 0
for i in range(len(s)):
for l in [i, i + 1]:
j = i
while j >= 0 and l < len(s) and s[j] == s[l]: j, l = j - 1, l + 1
if l - j - 1 > max_len: start, max_len = j + 1, l - j - 1
return s[start:start + max_len]
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