Longest Palindromic Substring Problem:
Given a string s, return the longest palindromic substring in s.
E.g.
Input: s = "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Questions
The below code is the solution of Errichto (a RedCoder of codeforces). It's an improvement from a O(N^3) solution of the same problem. On his youtube channel, he went step by step in gradually solve the problem more efficiently. But i have troubles in understanding his approach for this solution. My questions are:
- In the
good
function, what is the use ofL + x <= n
condition - Before doing the binary search, something related to parity seems to be done. Why? What is its contribution to the solution?
- The binary search here is kind of strange. I understand the basic binary search but do not get this variant. Can anyone explain it for me?
Code
bool is_palindrome(string s) {
string rev = s;
reverse(rev.begin(), rev.end());
return s == rev;
}
// returns true if there is a palindrome of length x
int good(int x, string s) {
int n = s.length();
for(int L = 0; L + x <= n; L++) {
if(is_palindrome(s.substr(L, x))) {
return L;
}
}
return -1;
}
class Solution {
public:
string longestPalindrome(string s) {
int best_len = 0;
string best_s = "";
int n = s.length();
for(int parity : {0, 1}) {
int low = 1, high = n;
if(low % 2 != parity) low++;
if(high % 2 != parity) high--;
while(low <= high) {
int mid = (low + high) / 2;
if(mid % 2 != parity) {
mid++;
}
if(mid > high) {
break;
}
int tmp = good(mid, s);
if(tmp != -1) {
if(mid > best_len) {
best_len = mid;
best_s = s.substr(tmp, mid);
}
low = mid + 2;
}
else {
high = mid - 2;
}
}
}
return best_s;
}
};