-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResult.java
More file actions
32 lines (26 loc) · 872 Bytes
/
Result.java
File metadata and controls
32 lines (26 loc) · 872 Bytes
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
package hackrank.algorithm.string.funny;
/**
* @see <a href="https://www.hackerrank.com/challenges/funny-string">Funny String</a>
*/
public class Result {
/**
* @param s value to check for funniness
* @return "Funny" or "Not Funny" value.
*/
public static String funnyString(String s) {
return isFunny(s) ? "Funny" : "Not Funny";
}
private static boolean isFunny(String value) {
if (value.length() < 2) {
return false;
}
char[] reverse = new StringBuilder(value).reverse().toString().toCharArray();
char[] characters = value.toCharArray();
for (int i = 1; i < characters.length; i++) {
if (Math.abs(characters[i] - characters[i - 1]) != Math.abs(reverse[i] - reverse[i - 1])) {
return false;
}
}
return true;
}
}