class Solution:
def longestPalindrome(self, s: str) -> str:
if not s or len(s) == 1:
return s
start, end = 0, 0
def expandAroundCenter(left: int, right: int) -> int:
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return right - left - 1
for i in range(len(s)):
len1 = expandAroundCenter(i, i) # Odd length palindrome
len2 = expandAroundCenter(i, i + 1) # Even length palindrome
max_len = max(len1, len2)
if max_len > end - start:
start = i - (max_len - 1) // 2
end = i + max_len // 2
return s[start:end+1]
class Solution {
public:
string longestPalindrome(string s) {
if (s.empty() || s.size() == 1) return s;
int start = 0, end = 0;
auto expandAroundCenter = [&](int left, int right) {
while (left >= 0 && right < s.size() && s[left] == s[right]) {
--left;
++right;
}
return right - left - 1;
};
for (int i = 0; i < s.size(); ++i) {
int len1 = expandAroundCenter(i, i); // Odd length
int len2 = expandAroundCenter(i, i + 1); // Even length
int len = max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substr(start, end - start + 1);
}
};
class Solution {
public String longestPalindrome(String s) {
if (s == null || s.length() < 1) return s;
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}
private int expandAroundCenter(String s, int left, int right) {
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
left--;
right++;
}
return right - left - 1;
}
}
var longestPalindrome = function(s) {
if (!s || s.length < 1) return s;
let start = 0, end = 0;
function expandAroundCenter(left, right) {
while (left >= 0 && right < s.length && s[left] === s[right]) {
left--;
right++;
}
return right - left - 1;
}
for (let i = 0; i < s.length; i++) {
let len1 = expandAroundCenter(i, i);
let len2 = expandAroundCenter(i, i + 1);
let len = Math.max(len1, len2);
if (len > end - start) {
start = i - Math.floor((len - 1) / 2);
end = i + Math.floor(len / 2);
}
}
return s.substring(start, end + 1);
};
Given a string s
, you are asked to find the longest substring of s
that is a palindrome.
A palindrome is a string that reads the same backward as forward (e.g., "racecar", "abba").
Constraints:
s
of length between 1 and 1000, consisting of only ASCII letters and digits.s
.
To solve this problem, the first idea that comes to mind is to check all possible substrings of s
and determine if each is a palindrome, keeping track of the longest one found. However, this brute-force approach is inefficient, especially for long strings, because there are O(n2) substrings and each check can take up to O(n) time.
We need to optimize. One insight is that palindromes "expand" from their center. Every palindrome can be defined by its center and its length. For each character (and the gap between each pair of characters), we can try to expand outward as long as the substring remains a palindrome. This reduces unnecessary checks and improves performance.
This leads us to the "expand around center" approach, which is both intuitive and efficient for this problem.
The optimized solution uses the "expand around center" technique. Here's how it works step by step:
i
, expand for both odd-length palindromes (centered at i
) and even-length palindromes (centered between i
and i+1
).start
to end
(inclusive).This approach ensures we check all possible palindromic substrings efficiently, without redundant checks.
Let's walk through the process with the input s = "babad"
:
The function can return either "bab" or "aba" as both are valid longest palindromic substrings.
The "Longest Palindromic Substring" problem can be solved efficiently using the expand around center technique. This approach leverages the symmetrical property of palindromes, checking all possible centers and expanding outwards to find the maximum-length palindrome. It is both intuitive and efficient, with a manageable O(n2) time complexity and O(1) space usage. The key insight is that all palindromes can be defined by their centers, avoiding redundant checks and unnecessary computations.