Update
Thanks for all the help! I've taken the edits below into consideration and re-factored to another passing solution:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
private static boolean isFunny (String s)
{
boolean stillEq = true;
final int FIRST_CHAR = 1;
final int LAST_CHAR = s.length() - 1;
for (int a = FIRST_CHAR, b = LAST_CHAR; a <= LAST_CHAR && b >= 0 && stillEq; ++a, --b)
{
int compare = (int)s.charAt(a) - (int)s.charAt(a-1);
int compareReverse = (int)s.charAt(b) - (int)s.charAt(b-1);
stillEq = Math.abs(compare) == Math.abs(compareReverse);
}
return stillEq;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tests = sc.nextInt();
for (int i = 0; i < tests; ++i)
{
String out = isFunny(sc.next()) ? "Funny" : "Not Funny";
System.out.println( out );
}
}
}
Main fixes were:
- do math in place on strings...no string reversal or tail call optimization problems
- fix bug of not comparing 1st and 0th characters in string and add constants to make code clearer