#C4095. Almost Palindrome Check
Almost Palindrome Check
Almost Palindrome Check
Given a string S, determine whether you can obtain a palindrome by removing at most one character from S. A palindrome is a string that reads the same forwards and backwards. Formally, a string S is almost a palindrome if there exists an index i such that the string obtained by removing S[i]
from S is a palindrome. Note that if the string is already a palindrome, it is considered almost a palindrome as well.
The typical approach is to use a two-pointer technique. Let the pointers be l and r beginning at the start and end of the string respectively. While S[l] equals S[r], move the pointers inward. If a mismatch is found, one can either remove S[l] or S[r] and check if the remaining substring is a palindrome. In short, you need to verify that either \( S[l+1..r] \) or \( S[l..r-1] \) is a palindrome.
inputFormat
The input consists of a single line containing a string S
which can include letters and other characters. The string may be empty.
outputFormat
Output a single line: True
if the string is almost a palindrome (or already a palindrome) after removing at most one character, or False
otherwise.
abca
True