Given a string s, return its longest palindromic substring. A substring is a contiguous part of the string, and a palindrome reads the same forward and backward.
If several palindromic substrings share the maximum length, return any one of them.
Example 1
Input: s = "babad"
Output: "bab"
Explanation: Both "bab" and "aba" are palindromes of length 3. Either answer is valid.
Example 2
Input: s = "cbbd"
Output: "bb"
Explanation: The two middle characters form a palindrome of length 2. The characters just outside them, c and d, differ, so the palindrome cannot grow.
Approach
A palindrome reads the same from both ends, so its characters match in pairs around its center. Starting at a center, we can compare the characters on its left and right. If they match, the palindrome grows, and we move outward to check the next pair.
For example, in "babad", start at the a at index 1. The characters on either side are both b, so the palindrome grows from "a" to "bab". We stop because moving farther left would take us outside the string.
An odd-length palindrome has a character at its center, while an even-length palindrome has a gap at its center. For example, "bb" in "cbbd" is centered between the two bs. Expanding farther compares c with d. They differ, so the expansion stops at "bb".
Every palindrome has one of these two kinds of centers. By trying both at every index and keeping the longest palindrome found, we can find the answer.
Algorithm
Initialise variables to store the start and length of the longest palindrome found.
At each index
i, expand from(i, i)to check odd-length palindromes and from(i, i + 1)to check even-length palindromes.While both pointers are within bounds and their characters match, move the left pointer one step left and the right pointer one step right.
When expansion stops, the palindrome runs from
left + 1throughright - 1, with lengthright - left - 1.If this palindrome is longer than the best one found, update the saved start and length.
After checking all centers, return the substring using the saved start and length.
Dry Run
Longest palindromic substring Expand around center
Code
#include <bits/stdc++.h>using namespace std;class Solution {private: // Return the start index and length of the longest palindrome at this center. pair<int, int> expandAroundCenter(const string& s, int left, int right) { int n = int(s.size()); // Expand while both pointers are in bounds and the characters match. while (left >= 0 && right < n && s[left] == s[right]) { --left; ++right; } // The last matching range runs from left + 1 through right - 1. return {left + 1, right - left - 1}; }public: // Find the longest palindromic substring. string longestPalindrome(string s) { int n = int(s.size()); // Track the start and length of the best palindrome found so far. int bestStart = 0; int bestLength = 0; for (int i = 0; i < n; ++i) { // Odd-length palindromes are centered on one character, as in "racecar". auto odd = expandAroundCenter(s, i, i); // Even-length palindromes are centered between two characters, as in "abba". auto even = expandAroundCenter(s, i, i + 1); // Compare the longer of these two expansions with the best so far. auto candidate = odd.second >= even.second ? odd : even; // A strict comparison keeps the earlier palindrome when lengths tie. if (candidate.second > bestLength) { bestStart = candidate.first; bestLength = candidate.second; } } // Extract the answer using its starting index and length. return s.substr(bestStart, bestLength); }};int main() { Solution sol; string s = "babad"; // Both "bab" and "aba" have length 3. The scan finds "bab" first. cout << sol.longestPalindrome(s) << '\n'; // Prints bab. return 0;}Time and Space Complexity
Time Complexity: O(N²) in the worst case. There are O(N) centers, and one expansion can take O(N) time. A string of repeated characters, such as "aaaaa", lets many centers expand several steps.
Space Complexity: O(1) for the expansion itself. The by-value parameter can copy O(N) characters, and the returned substring can also take up to O(N) space.
Interview follow-up Questions
Return the entire string. Every pair of characters matches, so expansion from the middle reaches both ends. Repeated characters also explain the O(n²) worst case, since many centers can expand far.
Be the first to add a comment.