-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunny_string.java
More file actions
63 lines (47 loc) · 1.27 KB
/
funny_string.java
File metadata and controls
63 lines (47 loc) · 1.27 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
//solution to: https://www.hackerrank.com/challenges/funny-string
public class funny_string {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int count = 0;
List<String> strings = new ArrayList<String>();
while (count < n) {
String word = scan.next();
count++;
String word_format = word.replaceAll("\\s+", "").toLowerCase();
strings.add(word_format);
}
isfunny(strings);
}
public static void isfunny(List<String> strings) {
String funny_string = "";
for (int j = 0; j < strings.size(); j++) {
int funny_count = funny(strings.get(j));
if (funny_count > 0) {
System.out.println("Not Funny");
} else if (funny_count == 0) {
System.out.println("Funny");
}
}
}
public static int funny(String str) {
int funny_count = 0;
for (int i = 0; i < str.length() - 1; i++) {
int tmp1 = str.charAt(i);
int tmp2 = str.charAt(i + 1);
int tmp3 = str.charAt(str.length() - 1 - i);
int tmp4 = str.charAt(str.length() - 1 - i - 1);
int r1 = Math.abs(tmp2 - tmp1);
int r2 = Math.abs(tmp3 - tmp4);
if (r1 != r2) {
funny_count++;
}
}
return funny_count;
}
}