#K14376. One-Reversal Palindromization

    ID: 24121 Type: Default 1000ms 256MiB

One-Reversal Palindromization

One-Reversal Palindromization

Given a string (s) of length (n), determine whether it is possible to transform (s) into a palindrome by performing exactly one reversal operation on a contiguous substring. A reversal operation means that you select a contiguous segment of (s) and reverse its characters. Note that if (s) is already a palindrome, you may perform a reversal on any single character (or any substring that does not change the string) and the answer is still considered valid.

The approach is to compare characters at symmetric positions and count the mismatches. Let (d) be the number of mismatched pairs. The answer is "YES" if one of the following conditions holds:

  1. (n = 1) (a single character is trivially a palindrome),
  2. (d = 0) (the string is already a palindrome),
  3. (d = 1) (one reversal can fix the single mismatch), or
  4. (d = 2) and (n) is odd (the middle character can help balance the reversal operation).

Otherwise, the answer is "NO".

inputFormat

The first line contains the integer (n) denoting the length of the string. The second line contains the string (s) consisting of lowercase English letters.

outputFormat

Output a single line containing either "YES" or "NO" indicating whether it is possible to make (s) a palindrome with exactly one reversal operation.## sample

5
adaba
YES

</p>