내용

글번호 611
작성자 heojk
작성일 2017-03-17 15:32:51
제목 [코딩 테스트] - The Love-Letter Mystery
내용 The Love-Letter Mystery John wants to write a love letter to his crush but is too shy to make it obvious. He decides to change all the words in the letter palindromes. so it can only be deciphered by someone sufficiently intelligent, passionate, and lucky to boot. To be this, he sets himeself two 2 rules: a. He can reduce the value of a letter, e.g. he can change 'd' to 'c', but he cannot change 'c' to 'd'. b. In order to form a palindrome, if can repeatedly reduce the value of any of the letters of a word, but only down to the letter 'a'. Each reduction in the value of any letter counts as a single operation. Complete the mystery function in your editor. It has 1 parameters: 1. A string array, letter, where letter_i denotes i_th love letter. It must return an integer array, where i_th element in the arrays denotes the minimum number of operations required to convert letter_i into a palindrome. Input Format The locked stub code in your editor reads the following input from stdin and passes in to your function. The first line contains an integer, T, denoting number of elements in letter. Next T line contains a string s in each line, where i_th string denotes letter_i. Constraints: . 1<= T <= 10 . 1 <= length of string <= pow(10, 4) . All characters are low case English letters. Output Format Your function must return an integer array. where i_th element in the arrays denotes the minimum number of operations required to convert letter_i into a palindrome. this is printed to stdout by the locked stub code in your editor. Sample Input 1 4 abc abcba abcd cba Sample Output 1 2 0 4 2 Explanation 1 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 = abca -> abba. For the fourth test case, cba -> bba -> aba. Sample Input 2 5 vpllamwr kfa hffv zycfjuvrhxf fdjsqlgmcux Sample Output 2 23 10 14 50 58 Code Stub import java.util.Scanner; public class TheLoveLetterMystery { public static void main(String[] args) { //This is stub code. Scanner stdin = new Scanner(System.in); int tests = stdin.nextInt(); String[] letters = new String[tests]; for(int i = 0; i < letters.length; i++) { letters[i] = stdin.next(); } stdin.close(); int[] result = mystery(letters); for(int count : result) { System.out.println(count); } } static int[] mystery(String[] letters) { //TODO. Write your code here. } }