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.
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.
Constraints
1<=q<=10
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
4 abc abcba abcd cba
Sample Output
2 0 4 2
Explanation
- For the first test case, abc → abb → aba.
- For the second test case, abcba is already a palindromic string.
- For the third test case, abcd → abcc → abcb → abca → abba.
- For the fourth test case, cba → bba → aba.
The Love-Letter Mystery HackerRank 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 HackerRank Solution in C++
#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 HackerRank 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 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]); else ops += (int)((byte)word[word.Length - 1 - i] - (byte)word[i]); } Console.WriteLine(ops); } } 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(); } Instance.NextMove(input); } } }
Attempt Love-Letter Mystery Hackerrank Challenge
Link – https://www.hackerrank.com/challenges/the-love-letter-mystery/
Next HackerRank Challenge Solution
Link – https://exploringbits.com/find-the-median-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.