-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
47 lines (42 loc) · 1.63 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
45
46
47
import java.util.*;
/*
We wil traverse thorough given char array from begigining of array and store abs(s[i]-s[i-1]) for each valid i in arr
now we will traverse the same string from end i.e. s.length-1 and store abs(s[i]-s[i-1]) for each valid i in rarr
now we compare each particular value of arr & rarr if there is mismatch we will print "Not Funny"
on the other hand we reach the end of both array means their all values match
Hence, we will return "Funny"
*/
public class solution {
private static String isFunny(char[] s){
int arr[]; //stores abs(s[i]-s[i-1]) for i=1 to i=s.length-1
int rarr[]; //stores abs(s[i]-s[i-1]) for i=s.length-1 to i=1
arr=new int[s.length];
rarr=new int[s.length];
for(int i = 1,j=0 ; i < s.length; i++,j++){
arr[j]=Math.abs(s[i]-s[i-1]);
}
int j=0;
for(int i = s.length-1 ; i >0; i--,j++){
rarr[j]=Math.abs(s[i]-s[i-1]);
}
for(int i=0;i<j; i++){
//if any value of both these array doesn't matches we return "Not Funny"
if(arr[i]!=rarr[i]){
return "Not Funny";
}
}
//If we have reached here means all the values of both the arrays have matched and means they are Funny
//Hence we will return "Funny"
return "Funny";
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i = 0; i < t; i++){
System.out.println(
isFunny( in.next().toCharArray() )
);
}
in.close();
}
}