Hackerrank - The Love-Letter Mystery Solution
2 min read

Hackerrank - The Love-Letter Mystery Solution

Hackerrank - The Love-Letter Mystery 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 , 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.

For example, given the string , the following two operations are performed: cdecddcdc.

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 , the number of queries.
The next  lines will each contain a string .

Constraints


| s |
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

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

Solution in Python

def theLoveLetterMystery(s):
    total = 0
    for i,j in enumerate(range(len(s)//2),1):
        a = s[ j]
        b = s[-i]
        total+= abs(ord(a)-ord(b))
    return tot

for _ in range(int(input())):
    print(theLoveLetterMystery(input()))

Additional Information

Variable a - Left part of string
Example aabb in aabbcbbaa or aabb in aabbbbaa
Variable b - Right part of string

Using deque

from collections import deque
def theLoveLetterMystery(s):
    s = deque((ord(i) for i in s))
    return sum(abs(s.pop()-s.popleft()) for i in range(len(s)//2))

for _ in range(int(input())):
    print(theLoveLetterMystery(input()))

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×