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.
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:
0if the string is already a palindrome,1if it can be made a palindrome by changing exactly one character,2otherwise.
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 consisting of lowercase English letters.
Output
- Print one integer:
0if is already a palindrome,1if it can be made a palindrome by changing exactly one character,2otherwise.
Output Format
Print a single integer as described above.
Constraints
- contains only lowercase English letters.
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.