Skip to main content
Back to problems
Codeforces
Easy
Strings
Math
An abandoned sentiment from past

Given a lowercase string, determine whether it is already a palindrome, can become one by changing exactly one character, or cannot be fixed that way.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Problem

You are given a lowercase string. Your task is to check whether the string is already a palindrome or can be turned into a palindrome by changing exactly one character.

A palindrome is a string that reads the same from left to right and from right to left.

Return:

  • 0 if the string is already a palindrome,
  • 1 if it can be made a palindrome by changing exactly one character,
  • 2 otherwise.

In this problem, changing a character means replacing it with any other character.

Notes

  • If the string already is a palindrome, no change is needed.
  • If more than one mismatch exists between mirrored positions, it cannot be fixed with just one change.

Input Format

Input

  • A single string ss consisting of lowercase English letters.

Output

  • Print one integer:
    • 0 if ss is already a palindrome,
    • 1 if it can be made a palindrome by changing exactly one character,
    • 2 otherwise.

Output Format

Print a single integer as described above.

Constraints

  • 1s1051 \le |s| \le 10^5
  • ss contains only lowercase English letters.
Examples
Sample cases returned by the problem API.

Example 1

Input

abca

Output

1

Explanation

Changing b to c or c to b makes the string a palindrome.

Example 2

Input

abba

Output

0

Explanation

The string is already a palindrome.

Show 1 more example

Example 3

Input

abcd

Output

2

Explanation

There are two mismatched mirrored pairs, so one replacement is not enough.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.