Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Hackerrank The Love Letter Mystery problem solution

YASH PAL, 31 July 202423 January 2026

Hackerrank The Love Letter Mystery problem solution – 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.

Function Description

Complete the theLoveLetterMystery function in the editor below.

theLoveLetterMystery has the following parameter(s):

  • string s: the text of the letter

Returns

  • int: the minimum number of operations

Input Format

The first line contains an integer q, the number of queries.
The next q lines will each contain a string s.

hackerrank the love letter mystery problem solution

Hackerrank The Love Letter Mystery problem solution in Python.

#!/usr/bin/env python

import sys


if __name__ == '__main__':
    T = int(sys.stdin.readline())
    
    for _ in range(T):
        s = list(sys.stdin.readline().strip())
        print(sum(abs(ord(s[i]) - ord(s[-i - 1])) for i in range(len(s) // 2)))

The Love Letter Mystery problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        try{
		  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		  String input = br.readLine();
        
		  while((input=br.readLine())!=null){
			 System.out.println(countConvertToPalindrome(input));
		  }
 
    	} catch(IOException io){
	   	   io.printStackTrace();
	    }	
    }
    
    public static int countConvertToPalindrome(String word) {
        char[] charArray = word.toCharArray();
        int operationCount = 0;
        for(int i = 0; i < charArray.length/2; i++) {
            int leftAsciiValue = (int) charArray[i];
            int rightAsciiValue = (int) charArray[charArray.length-i-1];
            if (leftAsciiValue < rightAsciiValue) {
                operationCount += rightAsciiValue - leftAsciiValue;
            } else {
                operationCount += leftAsciiValue - rightAsciiValue;
            }
        }
        return operationCount;
    }
    
}

The Love Letter Mystery problem solution in C++.

#include <cstdio>
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int T;
string cur;
int main(){
    cin>>T;
    for(int i=0; i<T; i++){
        cin>>cur;
        int ans=0;
        for(int i=0; i<cur.length()/2; i++){
            ans+=abs(cur[i]-cur[cur.length()-1-i]);
        }
        cout<<ans<<endl;
    }
    
}

The Love Letter Mystery problem solution in C.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX_STR_LEN 10000

int main(void) {
    int testCases, i, j, lenstr, numOps;
    char word[MAX_STR_LEN];
    scanf("%d", &testCases);
    if (testCases > 10 || testCases < 1){
        fprintf(stderr,"Usage: T out of boundsn");
        return 1;
    }

    for(i = 0; i < testCases; ++i) {
        numOps = 0;
        scanf("%s", word);
        lenstr = (int) strlen(word);
        for(j = 0; j < lenstr/2; ++j) {
            numOps += abs(tolower(word[j]) - tolower(word[lenstr - j - 1]));
        }
        printf("%dn",numOps);
    }

    return 0;
}

Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var __input_stdin = "";
var __input_stdin_array = "";
var __input_currentline = 0;
var unicode_min = "a".charCodeAt(0);
var unicode_max = "z".charCodeAt(0);

process.stdin.on('data', function (data) {
    __input_stdin += data;
});

function computePalindrome(astring) {
    astring = astring.toLowerCase();
    //process.stdout.write(astring+'n');
    var N = astring.length;
    var center_right = Math.ceil(N/2);
    var center_left = Math.floor(N/2 - 1);
    var num_ops = 0;
    for (var i = center_right, j = center_left; i < N; i++, j--) {
        //process.stdout.write(' i: '+ i);
        //process.stdout.write(' j: '+ j);
        var diff = astring.charCodeAt(i) - astring.charCodeAt(j);
        num_ops += Math.abs(diff);
    };
    process.stdout.write(num_ops + 'n');
}
process.stdin.on('end', function () {
    __input_stdin_array = __input_stdin.split("n");
    T = __input_stdin_array[0];
    for (var i =1; i <= T; i++) {
        computePalindrome(__input_stdin_array[i]);
    };
});

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes