#P10905. Magic Palindrome Transformation
Magic Palindrome Transformation
Magic Palindrome Transformation
Given a string \(S\) consisting only of lowercase letters, you are allowed to prepend any number (possibly zero) of characters chosen from the set \(\{l, q, b\}\) (with ASCII codes 108, 113, and 98 respectively) to form a new string \(T\). Determine if it is possible to obtain a palindrome string \(T\) through such an operation.
A palindrome is a string that reads the same forwards and backwards. Note that the prepended characters must come only from the set \(\{l, q, b\}\).
Formally, find if there exists a non-negative integer \(k\) and a string \(P\) of length \(k\) (where every character of \(P\) is in \(\{l, q, b\}\)) such that:
\[ P+S = \text{reverse}(P+S)\,. \]If \(S\) is already a palindrome, then the answer is YES
(by choosing \(k=0\)). Otherwise, you might be able to choose a suitable \(k\) so that the remaining part of \(S\) becomes a palindrome after the forced prefix conditions. In particular, when you prepend \(k\) characters, the first \(k\) characters of \(P+S\) are fixed by your choice, and they must mirror the last \(k\) characters of \(P+S\) (which come from \(S\)). Thus, for any \(1 \le k \le |S|\), you must have that the last \(k\) characters of \(S\) are all in \(\{l, q, b\}\) and that the remaining prefix of \(S\) (if any) is itself a palindrome. Additionally, if \(k > |S|\), then the prefix that mirrors \(S\) forces every character of \(S\) to belong to \(\{l, q, b\}\>.
inputFormat
The input consists of a single line containing the string \(S\) (non-empty), which contains only lowercase English letters.
outputFormat
Output YES
if it is possible to transform \(S\) into a palindrome by prepending characters from \(\{l, q, b\}\); otherwise, output NO
.
sample
aba
YES