Almost-Palindromic Substring

You are given a string s composed entirely of lowercase English characters. A substring qualifies as almost-palindromic if it can be transformed into a perfect palindrome by deleting exactly one of its characters. Your task is to calculate and return the length of the longest almost-palindromic substring hidden within s.

Example 1:

Input: s = "racexcar"

Output: 8

Explanation: The longest almost-palindromic substring is the entire string "racexcar" itself. By removing the character 'x', the remaining string correctly forms "racecar", which is a valid palindrome. Because the entire string satisfies the condition, the maximum length is 8.

Example 2:

Input: s = "abcdef"

Output: 2

Explanation: In this string of entirely unique characters, no substring of length 3 or more can become a palindrome by removing exactly one character. However, any 2-character substring (such as "ab") becomes a single-character string when one letter is removed, which is by definition a palindrome. Therefore, the longest valid length defaults to 2.

Now Your Turn!

Pick the correct output for the given input

Input: s = "qwelevelrt"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 <= s.length <= 2500
  • s consists of only lowercase English letters.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
// --- IDE Code ---
class Solution {
public:
int almostPalindromic(string s) {
// Write your code here
return 0;
}
};
 
Test Case

Input:

S