At Hackerrank.com, the love letter mystery is a simple programming problem, which I already solved.
My solution involved calculating the palindrome and counting the number of operations to do it. On Hackerrank, they have another approach, which I don't understand completely:
Given a string, you have to convert it into a palindrome using a certain set of operations.
Let's assume that the length of the string is L, and the characters are indexed from 0 to L-1. The string will be palindrome, only if the characters at index i and the characters at index (L-1-i) are same. So, we need to ensure that the characters for all such indices are same.
Let's assume that one such set of indices can be denoted by A' and B'. It is given that it takes one operation in decreasing value of a character by 1. So, the total number of operations will be |A'-B'|.We need to calculate this for all such pairs of indices, which can be done in a single for loop. Take a look at the setter's code.
I understand everything up until they mention that the total number of operations are |A'-B'|, the absolute value between the two indices. Why is that? For some reason, my head is being thicker (than usual).
Thanks!
======== EDIT ==========
Hey guys, thanks for your input. I thought that this stackexchange site would had been more appropiate than stackoverflow. Is there any stackexchange site where this question is more appropiate?