The Love-Letter Mystery

Problem source:

James found a love letter his friend Harry has written for his girlfriend. James is a prankster, so he decides to meddle with the letter. He changes all the words in the letter into palindromes.

To do this, he follows two rules:

  1. He can reduce the value of a letter, e.g. he can change d to c, but he cannot change c to d.
  2. In order to form a palindrome, if he has to repeatedly reduce the value of a letter, he can do it until the letter becomes a. Once a letter has been changed to a, it can no longer be changed.

Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome.

Input Format

The first line contains an integer T, i.e., the number of test cases.
The next T lines will contain a string each. The strings do not contain any spaces.

1 length of string 104
All characters are lower case English letters.

Output Format

A single line containing the number of minimum operations corresponding to each test case.

Sample Input


Sample Output

class Love_Letter_Mystery
static void Main(string[] args)
int testCases = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < testCases; ++i)
string strInput = Console.ReadLine();

static int PalindromeCount(string strInput)
int counter = 0, start = 0, end = strInput.Length – 1;
while (start < end)
if (strInput[start] != strInput[end])
counter += Math.Abs(strInput[end] – strInput[start]);

return counter;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s