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
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
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
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
I'm a little rusty in javaJava, but I'm using it to get ready for interviews. Here's the problem description from hackerrankHackerRank:
Problem StatementProblem Statement
Suppose you have a string \$S\$ which has length \$N\$ and is indexed from \0ドル\$ to \$N−1\$. String \$R\$ is the reverse of the string \$S\$. The string \$S\$ is funny if the condition \$|S_i−S_{i−1}|=|R_i−R_{i−1}|\$ is true for every \$i\$ from 1 to \$N−1\$.
(Note: Given a string str, stri denotes the ascii value of the ith character (0-indexed) of str. |x| denotes the absolute value of an integer x)
Input FormatInput Format
First line of the input will contain an integer T. T testcases follow. Each of the next T lines contains one string S.
Constraints
1<=T<=10Constraints
2<=length of S<=10000
- \1ドル \le T \le 10\$
- \2ドル \le\$ length of S \$\le 10000\$
Output FormatOutput Format
For each string, print Funny or Not Funny in separate lines.
Below is myThis passing solution. This took me about 20 minutes, so that might be a bit long given the difficulty of the problem. I'm open to critiques on my speed too.
I'm a little rusty in java, but I'm using it to get ready for interviews. Here's the problem description from hackerrank:
Problem Statement
Suppose you have a string \$S\$ which has length \$N\$ and is indexed from \0ドル\$ to \$N−1\$. String \$R\$ is the reverse of the string \$S\$. The string \$S\$ is funny if the condition \$|S_i−S_{i−1}|=|R_i−R_{i−1}|\$ is true for every \$i\$ from 1 to \$N−1\$.
(Note: Given a string str, stri denotes the ascii value of the ith character (0-indexed) of str. |x| denotes the absolute value of an integer x)
Input Format
First line of the input will contain an integer T. T testcases follow. Each of the next T lines contains one string S.
Constraints
1<=T<=10
2<=length of S<=10000
Output Format
For each string, print Funny or Not Funny in separate lines.
Below is my passing solution. This took me about 20 minutes, so that might be a bit long given the difficulty of the problem. I'm open to critiques on my speed too.
I'm a little rusty in Java, but I'm using it to get ready for interviews. Here's the problem description from HackerRank:
Problem Statement
Suppose you have a string \$S\$ which has length \$N\$ and is indexed from \0ドル\$ to \$N−1\$. String \$R\$ is the reverse of the string \$S\$. The string \$S\$ is funny if the condition \$|S_i−S_{i−1}|=|R_i−R_{i−1}|\$ is true for every \$i\$ from 1 to \$N−1\$.
(Note: Given a string str, stri denotes the ascii value of the ith character (0-indexed) of str. |x| denotes the absolute value of an integer x)
Input Format
First line of the input will contain an integer T. T testcases follow. Each of the next T lines contains one string S.
Constraints
- \1ドル \le T \le 10\$
- \2ドル \le\$ length of S \$\le 10000\$
Output Format
For each string, print Funny or Not Funny in separate lines.
This passing solution took me about 20 minutes, so that might be a bit long given the difficulty of the problem. I'm open to critiques on my speed too.