Header Ad

Hackerrank The Love Letter Mystery problem solution

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


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


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


Post a Comment

0 Comments