niladri mandal
niladri mandal

Reputation: 11

timings problems in hackerrank in "The Love-Letter Mystery" question

I am solving the "The Love-Letter Mystery" problem may be my logic is correct but it is showing the timings problems The Question is Question here.My solution for same is given below. It contains two functions one is theLoveLetterMystery(String s) which is returning sum_minimum_Steps and other is conversionCount(String s,int i,int j) which is returning int characterCount variable.It sums all the minimum steps to return the value

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the theLoveLetterMystery function below.
    static int theLoveLetterMystery(String s) {
        int startCounter=0,endCounter=(s.length()-1),sum_minimum_Steps=0;
        // s.charAt(startCounter)!=s.charAt(endCounter)
        while(startCounter!=endCounter)
        {
            if(s.charAt(startCounter)!=s.charAt(endCounter))
            {
                //minimun steps function executes
                sum_minimum_Steps+=conversionCount(s,startCounter,endCounter);
            }else{
                startCounter++;
                endCounter--;
            }
        }
        return sum_minimum_Steps;
    }
    static int conversionCount(String s,int i,int j) {
        int charStartAscii=(int)s.charAt(i);
        int charEndAscii=(int)s.charAt(j);
        int characterCount=0;
        while(charStartAscii!=charEndAscii)
        {
        charEndAscii--;
        characterCount++;
        }
        return characterCount;
    }    
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int q = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int qItr = 0; qItr < q; qItr++) {
            String s = scanner.nextLine();

            int result = theLoveLetterMystery(s);

            bufferedWriter.write(String.valueOf(result));
            bufferedWriter.newLine();
        }

        bufferedWriter.close();

        scanner.close();
    }
}

Upvotes: 0

Views: 225

Answers (2)

user3599005
user3599005

Reputation: 69

function theLoveLetterMystery($s) {
    $s = strtoupper($s);
    $alpha = array('A','B','C','D','E','F','G','H','I','J','K', 'L','M','N','O','P','Q','R','S','T','U','V','W','X ','Y','Z');
$alpha_flip = array_flip($alpha);
    // Write your code here
        $i = 0;
        $j = strlen($s)-1;
        $sol = 0;
        while($i<$j){
             $sol += abs($alpha_flip[$s[$i]]-$alpha_flip[$s[$j]]);
            ++$i;
            --$j;
        }
        return $sol;

}
echo theLoveLetterMystery('abbc')// return 2

Upvotes: 0

dariosicily
dariosicily

Reputation: 4547

Starting from the assumption we are working on an alphabet of 26 letters(range a - z) "The Love-Letter Mystery" question is about find the minimum number of operations consisting of decrement of value 1 of a letter (for example d -> c and excluding letter a) to convert a string to a palindrome string. This can be obtained adding the absolute int differences between chars standing in positions i and n - i - 1 where n is the length of the string and iterating over half of the string. Below the code:

public static int conversionCount(String s) {
    char[] arr = s.toCharArray();
    int length = s.length();

    int count = 0;
    for (int i = 0; i < length / 2; ++i) {
            count += Math.abs((int) (arr[i] - arr[length - i - 1]));
    }
    return count;
}

Note: I tested it in hackerrank passing all tests.

Upvotes: 1

Related Questions