[Hackerrank] the love-letter mystery

Source: Internet
Author: User

James found his friend Harry's love letter for his girlfriend. James is very fond of pranks, so he decided to hook up. He converted every word in the letter into a reply. For any given string, it can reduce the value of any character. For example, 'D' can be changed to 'C', which is an operation. (In addition, he can only reduce the character value to 'A', and 'A' cannot be reduced to 'Z '). Find the minimum number of operations required to convert a given string to a forward object.

Input Format
The first line contains integers.TIndicates the number of groups of test data.
NextTEach row contains a string.

Output Format
Each row of test data is output, indicating the minimum number of operations required for this data.

Value Range
1 ≤T≤ 10
1 ≤String Length≤ 104

Question: a very simple question. At first, I thought about DFS =. =

To convert a string to a text return, the characters on I and l-I must be the same, so you only need to calculate all ABS (s [I]-s [L-I]). and.

 1 import java.io.*; 2 import java.util.*; 3 import java.text.*; 4 import java.math.*; 5 import java.util.regex.*; 6  7 public class Solution { 8    static int The_Love_Letter_Mystery(String s){ 9        int sum = 0;10        if(s.length() <= 1)11            return 0;12        int start = 0;13        int end = s.length()-1;14        while(start<end){15            sum += Math.abs(s.charAt(start) - s.charAt(end));16            start++;17            end--;18        }19        return sum;20    }21 22 23  public static void main(String[] args) {24      Scanner in = new Scanner(System.in);25      int t = in.nextInt();26      for(int i = 0;i < t;i++){27          String string = in.next();28          System.out.println(The_Love_Letter_Mystery(string));29      }30 31      32    }33 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.