-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFunnyString.java
37 lines (31 loc) · 1.21 KB
/
FunnyString.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
// https://www.hackerrank.com/challenges/funny-string/problem
package strings;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FunnyString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
while (queries-- > 0) {
String string = scanner.next();
System.out.println(isFunny(string) ? "Funny" : "Not Funny");
}
}
private static boolean isFunny(String string) {
String reversed = reverse(string);
List<Integer> stringAsciiDifference = difference(string);
List<Integer> reverseAsciiDifference = difference(reversed);
return stringAsciiDifference.equals(reverseAsciiDifference);
}
private static String reverse(String string) {
return new StringBuilder(string).reverse().toString();
}
private static List<Integer> difference(String string) {
List<Integer> result = new ArrayList<>(string.length() - 1);
for (int index = 0 ; index < string.length() - 1 ; index++) {
result.add(Math.abs(string.charAt(index) - string.charAt(index + 1)));
}
return result;
}
}