I'm trying to determine the number of times I have to change each specific character in a string to make it a palindrome. You can only change a character one at a time from the end.
Example: "abc" -> "abb" -> "aba" should print 2. "aba" will print 0 because it's already a palindrome. "abcd" -> "abcc" -> "abcb" -> "abca" -> "abba" will print 4 because it took 4 changes to make a palindrome.
I'm not too sure how to approach this - I figured out the case where if it's a palindrome (if reversed string is the same) then it'll print out a 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main() {
int number;
cin >> number; //expecting a number for first line user input
for (int i = 0; i < number; i++) {
string str;
int j = 0;
int count = 0; //determines the number of times a letter has to be reduced
string reversed;
cin >> str; //takes in every word
reversed = string(str.rbegin(), str.rend()); //reverses the string
if (reversed == str) //if palindrome, print out 0
cout << "0" << endl;
else { //if not palindrome, determine how many times it takes to make it a palindrome
//STUCK HERE
}
}
}
It looks like you just want to add/subtract the character values in the right half of the string to make them get closer to the character values in the left half of the string.