Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

Hackerrank The Love Letter Mystery problem solution

YASH PAL, 31 July 2024

In this Hackerrank The Love-Letter Mystery problem we have given a string and we need to find the minimum number of operations required to convert a given string into a palindrome number.

hackerrank the love letter mystery problem solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

#!/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)))

Problem solution in Java Programming.

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;
    }
    
}

Problem solution in C++ programming.

#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;
    }
    
}

Problem solution in C programming.

#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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes