CodeforcesSep 13, 2025

Mike and palindrome

Hazrat Ali

Codeforces

A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.

Input

The first and single line contains string s (1 ≤ |s| ≤ 15).

Output

Print "YES" (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or "NO" (without quotes) otherwise.

Examples
Input
abccaa
Output
YES
Input
abbcca
Output
NO
Input
abcda
Output
YES

Solution
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  cin >> s;
  if (s.size() == 1) {
    cout << "YES" << endl;
    return 0;
  }
  bool palindrome = true;
  int cnt = 1;
  for (int i = 0; i < s.size() / 2; i++) {
    if (s[i] != s[s.size() - 1 - i]) {
      if (cnt > 0) {
        cnt--;
      } else {
        palindrome = false;
        break;
      }
    }
  }
  if (s.size() % 2 == 1 and cnt == 1) {
    cnt--;
  }
  if (palindrome and cnt == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}



Comments