HackerRank The Love-Letter Mystery Solution Yashwant Parihar, April 26, 2023May 6, 2023 In this post, we will solve 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: 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. 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.Example8 = cdeThe 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. Sample Input STDIN Function ----- -------- 4 q = 4 abc query 1 = 'abc' abcba abcd cba Sample Output 2 0 4 2 Explanation For the first query, abc → abb → aba. For the second query, abcba is already a palindromic string. For the third query, abcd→ abcc→ abcb → abca → abba. For the fourth query, cba → bba → aba. HackerRank The Love-Letter Mystery Problem Solution The Love-Letter Mystery C Solution #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 bounds\n"); 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("%d\n",numOps); } return 0; } The Love-Letter Mystery C++ Solution #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int length; cin>>length; for(int i = 0; i < length; ++i){ int red = 0; string s; cin>>s; for(int j = 0; j < s.length()/2; ++j){ red += abs((char)s[j]-(char)s[s.length()-j-1]); } cout<<red<<endl; } return 0; } The Love-Letter Mystery C Sharp Solution using System; using System.Linq; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { int numCases = Convert.ToInt32(Console.ReadLine()); for(int i = 0; i < numCases; i++) { string source = Console.ReadLine().Trim(); int halfLength = Convert.ToInt32(Math.Floor((double)source.Length / 2.0)); int count = 0; char[] firstHalf = source.Substring(0, halfLength).ToCharArray(); char[] secondHalf = source.Substring(source.Length - halfLength, halfLength).ToCharArray(); int secondHalfMaxIndex = secondHalf.Length-1; for(int x = 0; x < halfLength; x++) { if(firstHalf[x] == secondHalf[secondHalfMaxIndex - x]) { continue; } // do nothing else if (firstHalf[x] > secondHalf[secondHalfMaxIndex - x]) // decrement letter in first half { while(firstHalf[x] > secondHalf[secondHalfMaxIndex - x]) { firstHalf[x] = (char)( (int)firstHalf[x] - 1 ); count++; } } else if (firstHalf[x] < secondHalf[secondHalfMaxIndex - x]) // decrement letter in second half { while(firstHalf[x] < secondHalf[secondHalfMaxIndex - x]) { secondHalf[secondHalfMaxIndex - x] = (char)( (int)secondHalf[secondHalfMaxIndex - x] - 1 ); count++; } } } Console.WriteLine(count); } } } The Love-Letter Mystery Java Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'theLoveLetterMystery' function below. * * The function is expected to return an INTEGER. * The function accepts STRING s as parameter. */ public static int theLoveLetterMystery(String s) { // Write your code here var charArr = s.toCharArray(); var totalCount = 0; for (var i=0; i<s.length()/2; i++) { totalCount += Math.abs(charArr[i] - charArr[s.length()-1-i]); } return totalCount; } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int q = Integer.parseInt(bufferedReader.readLine().trim()); IntStream.range(0, q).forEach(qItr -> { try { String s = bufferedReader.readLine(); int result = Result.theLoveLetterMystery(s); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); } catch (IOException ex) { throw new RuntimeException(ex); } }); bufferedReader.close(); bufferedWriter.close(); } } The Love-Letter Mystery JavaScript Solution 'use strict'; var alphabet = "abcdefghijklmnopqrstuvwxyz"; function diff(a, b) { a = alphabet.indexOf(a); b = alphabet.indexOf(b); return Math.abs(a - b); } function calc(word) { var operations = 0; var half = Math.floor(word.length / 2); var l = word.length-1; if(word.length === 1) return 0; for(var i = 0; i < half; i++) { var a = word.charAt(i); var b = word.charAt(l-i); operations += diff(a, b); } return operations; } function processData(input) { var lines = input.split('\n'); var ans = ""; for(var i = 1; i < lines.length; i++) { ans += calc(lines[i]) + "\n"; } process.stdout.write(ans); } 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 Python Solution import sys from operator import abs def main(): t = sys.stdin.readline().strip() for i in range(int(t)): testcase = sys.stdin.readline().strip() cnt = 0 for j in range(int(len(testcase) / 2)): cnt = cnt + abs(ord(testcase[j]) - ord(testcase[len(testcase) - 1 - j])) print(cnt) if __name__ == "__main__": main() Other Solutions HackerRank Find the Median Problem Solution HackerRank Insertion Sort Advanced Analysis c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython