While on SO someone posted this question and I gave it a quick try. My solution passed all the testcases.
Link to the problem: Problem: (A) Mike and palindrome
Question:
Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome.
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
CODE
#include <string>
#include <iostream>
int main(){
std::string str;
std::cin >> str;
int i{0}, j{str.length() - 1}, cnt{0};
// Check from both the ends if characters are equal.
while(i < j){
if (str[i]!= str[j]) ++cnt;
++i;
--j;
}
// If there's one mismatch.
if ( cnt == 1 ) std::cout << "YES";
// Odd length string with 0 mismatches e.g: abcba, can change middle char.
else if ( cnt == 0 && str.length()&1 ) std::cout << "YES";
else std::cout << "NO";
}