The Love-Letter Mystery HackerRank Solution in C, C++, Java, Python

James found a love letter that his friend Harry has written to 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 only reduce the value of a letter by 1, i.e. he can change d to c, but he cannot change c to d or d to b.
  2. The letter a may not be reduced any further.

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.

For example, given the string s=cde, the following two operations are performed: cde → cdd → cdc.

Function Description

Complete the theLoveLetterMystery function in the editor below. It should return the integer representing the minimum number of operations needed to make the string a palindrome.

theLoveLetterMystery has the following parameter(s):

  • s: a string

Input Format

The first line contains an integer q, the number of queries.

The next q lines will each contain a string s.



1<= | s |<=10^4

All strings are composed of lower case English letters, *ascii[a-z], with no spaces.

Output Format

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

Sample Input







Sample Output







  1. For the first test case, abc → abb → aba.
  2. For the second test case, abcba is already a palindromic string.
  3. For the third test case, abcd → abcc → abcb → abca → abba.
  4. For the fourth test case, cba → bba → aba.

The Love-Letter Mystery HackerRank Solution in C


int T,n,ans,i;
char s[20000];

int main()


The Love-Letter Mystery HackerRank Solution in C++

using namespace std;

int main(){
    int t;
    cin >> t;
        string s;
        cin >> s;
        int i = 0;
        int j = s.length()-1;
        int sol = 0;


The Love-Letter Mystery HackerRank Solution in Java

import java.util.Scanner;

public class Solution {

     * @param args
    public static void main(String[] args) 
        Scanner scan = new Scanner(;
        int T = scan.nextInt();scan.nextLine();
        for(int i=0;i


The Love-Letter Mystery HackerRank Solution in Python

for t in xrange(input()):
    s = raw_input().strip()
    res = 0
    for i in xrange(len(s)/2):
        res += abs(ord(s[i]) - ord(s[-i-1]))
    print res


The Love-Letter Mystery HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace subROUTINE.HackerRank.WeeklyChallenge3
    public class Challenge1
        private static readonly Challenge1 Instance = new Challenge1();

        public void NextMove(string[] input)
            foreach (var word in input)
                var maxIndex = word.Length / 2;
                int ops = 0;
                for (int i = 0; i < maxIndex; i++)
                    if (word[i] >= word[word.Length - 1 - i])
                        ops += (int)((byte)word[i] - (byte)word[word.Length - 1 - i]);
                        ops += (int)((byte)word[word.Length - 1 - i] - (byte)word[i]);


        static void Main(string[] args)
            var testCaseCount = Int32.Parse(Console.ReadLine());

            var input = new string[testCaseCount];
            for (int i = 0; i < testCaseCount; i++)
                input[i] = Console.ReadLine();




Attempt Love-Letter Mystery Hackerrank Challenge

Link –

Next HackerRank Challenge Solution 

Link –

Leave a Comment