forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
44 lines (38 loc) · 1.18 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Problem: https://www.hackerrank.com/challenges/the-love-letter-mystery
//Java 8
/*
Initial Thoughts:
Iterate through the string with pointers at
the start and end of the string. At each
iteration check which one is greater and
reduce that one by their difference in
ASCII value. Add that difference to the
operations counter
Time complexity: O(n)
Space complexity: O(1)
*/
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. */
Scanner input = new Scanner(System.in);
int T = input.nextInt();
input.nextLine();
tests:
for(int t = 0; t < T; t++)
{
String s = input.nextLine();
int operationsPerformed = 0;
int i = 0;
int j = s.length() - 1;
while(i < j)
{
operationsPerformed += Math.abs(s.charAt(i) - s.charAt(j));
i++;
j--;
}
System.out.println(operationsPerformed);
}
}
}