HackerRank The Love Letter Mystery Solution

Hello Programmers, In this post, you will learn how to solve HackerRank The Love Letter Mystery Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank The Love Letter Mystery Solution
HackerRank The Love Letter Mystery Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank The Love Letter Mystery Solution

Task

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.

Example

s = cde

The following two operations are performed: cde → cdd → cdc. Return 2.

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.

Constraints

  • 1 <= q <= 10
  • 1 <= |s| <= 104
  • All strings are composed of lower case English letters, ascii[az], with no spaces.

Sample Input

STDIN   Function
-----   --------
4       q = 4
abc     query 1 = 'abc'
abcba
abcd
cba

Sample Output

2
0
4
2

Explanation

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

HackerRank The Love Letter Mystery Solution

The Love Letter Mystery Solution in C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int T,n,ans,i;
char s[20000];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",&s);
        n=strlen(s);
        for(ans=i=0;i<n-1-i;i++)
            ans+=abs(s[i]-s[n-1-i]);
        printf("%d\n",ans);
    }
    return 0;
}

The Love Letter Mystery Solution in Cpp

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        int i = 0;
        int j = s.length()-1;
        int sol = 0;
        while(i<j){
            sol += abs(s[i]-s[j]);
            ++i;
            --j;
        }
        cout<<sol<<"\n";
    }
    return 0;
}

The Love Letter Mystery Solution in Java

import java.util.Scanner;
public class Solution {
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		Scanner scan = new Scanner(System.in);
		int T = scan.nextInt();scan.nextLine();
		
		for(int i=0;i<T;i++)
		{
			String s = scan.nextLine();
			int count=0;
			for(int j=0;j<s.length()/2;j++)
				count+=Math.abs(s.charAt(j)-s.charAt(s.length()-1-j));
			System.out.println(count);
		}
	}
}

The Love Letter Mystery 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 Solution using JavaScript

'use strict';
function processData(input) {
    var parse_fun = function (s) { return parseInt(s, 10); };
    var lines = input.split('\n');
    var T = parse_fun(lines.shift());
    for (var t = 0; t < T; t++) {
        var s = lines[t].trim();
        var res = 0;
        for (var i = 0; i < s.length / 2; i++) {
            res += Math.abs(s.charCodeAt(i) - s.charCodeAt(s.length - 1 - i));
        }
        console.log(res);
    }
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
var _input = "";
process.stdin.on("data", function (input) { _input += input; });
process.stdin.on("end", function () { processData(_input); });

The Love Letter Mystery Solution in Scala

object Solution extends App {
  for (t <- 1 to readInt) {
    val s = readLine
    val r = s.reverse
    val c = (s zip r).map { case (x, y) => math.max(0, x - y) }.sum
    println(c)
  }
}

The Love Letter Mystery Solution in Pascal

program letter;
var
	S:ansistring;
	T:smallint;
	i,j,k:integer;
	sum:longint;
function alphanum(c:char):integer;
begin
	case c of
		'a':alphanum:=1;
		'b':alphanum:=2;
		'c':alphanum:=3;
		'd':alphanum:=4;
		'e':alphanum:=5;
		'f':alphanum:=6;
		'g':alphanum:=7;
		'h':alphanum:=8;
		'i':alphanum:=9;
		'j':alphanum:=10;
		'k':alphanum:=11;
		'l':alphanum:=12;
		'm':alphanum:=13;
		'n':alphanum:=14;
		'o':alphanum:=15;
		'p':alphanum:=16;
		'q':alphanum:=17;
		'r':alphanum:=18;
		's':alphanum:=19;
		't':alphanum:=20;
		'u':alphanum:=21;
		'v':alphanum:=22;
		'w':alphanum:=23;
		'x':alphanum:=24;
		'y':alphanum:=25;
		'z':alphanum:=26;
	end;
end;
begin;
	readln(T);
	for i:=1 to T do
	begin
		readln(S);
		k:=length(s); sum:=0;
		for j:=1 to (length(s) div 2) do
		begin
			sum:=sum+ abs(alphanum(S[j])-alphanum(S[k-j+1]));
		end;
		writeln(sum);
	end;
	readln;
end.

Disclaimer: This problem (The Love Letter Mystery) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Determining DNA Health Solution

Leave a Reply

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