Hi,
Please take a look at this question and my solution (just the funnyString() function) and let me know what you think.
In this challenge, you will determine whether a string is funny or not. To determine whether a string is funny, create a copy of the string in reverse e.g. . Iterating through each string, compare the absolute difference in the ascii values of the characters at positions 0 and 1, 1 and 2 and so on to the end. If the list of absolute differences is the same for both strings, they are funny.
Determine whether a give string is funny. If it is, return Funny, otherwise return Not Funny.
For example, given the string , the ordinal values of the charcters are . and the ordinals are . The absolute differences of the adjacent elements for both strings are , so the answer is Funny.
Function Description
Complete the funnyString function in the editor below. For each test case, it should return a string, either Funny or Not Funny.
funnyString has the following parameter(s):
s: a string to test
Input Format
The first line contains an integer , the number of queries.
The next lines each contain a string, .
Constraints
Output Format
For each string print whether it is Funny or Not Funny on a new line.
Sample Input
2
acxz
bcxz
Sample Output
Funny
Not Funny
Explanation
You can use to store the reverse of .
Test Case 0:
,
Corresponding ASCII values of characters of the strings:
and
For both the strings the adjacent difference list is [2, 21, 2] so we print Funny.
Test Case 1:
,
Corresponding ASCII values of characters of the strings:
and
The adjacent difference list for string is [1, 21, 2] and for string it is [2, 21, 1]. Since they are not the same we print Not Funny.
import java.io.;
import java.math.;
import java.security.;
import java.text.;
import java.util.;
import java.util.concurrent.;
import java.util.regex.*;
public class Solution {
// Complete the funnyString function below.
static String funnyString(String s) {
//The algorithm is as follows:
//A. Check and make sure s is between 2 and 10000 (inclusive) in length
//B. Then
// i) Produce an array of the absolute differences in the ascii values of s's adjacent characters
// ii) Produce an array of the absolute differences in the ascii values of s-reversed's adjacent characters
// iii) if the 2 arrays in i) and ii) are the same then they are funny, otherwise they aren't
//The implementation of my algorithm is as follows
//A.
int theLengthOfS = s.length();
if ((theLengthOfS < 2) || (theLengthOfS > 10000)){
System.out.print("Please make sure s is between 2 and 10,000 (inclusive) in length. Thanks.");
return null;
}
//B. i)
int[] myAsciiArrayOfS = new int[theLengthOfS];
int[] myDifferencesArrayOfS = new int[theLengthOfS -1];
//this puts the ascii values of s's characters into the array
for(int i = 0; i < theLengthOfS; i++){
myAsciiArrayOfS[i] = (int)((char)(s.charAt(i)));
}
System.out.println(" ");
System.out.println("myAsciiArrayOfS is: " + myAsciiArrayOfS);
//this puts the differences of ascii values of s's characters into the array
for(int k = 0; k < myAsciiArrayOfS.length - 1; k++){
myDifferencesArrayOfS[k] = Math.abs(myAsciiArrayOfS[k+1] - myAsciiArrayOfS[k]);
}
System.out.println("myDifferencesArrayOfS is: " + myDifferencesArrayOfS);
//B. ii)
//Reverse my string
char mySCharArray[] = s.toCharArray();
char myReversedSCharArray[] = new char[theLengthOfS];
for(int r = 0; r < theLengthOfS; r++){
myReversedSCharArray[theLengthOfS - r - 1] = mySCharArray[r];
}
String myReversedS = String.copyValueOf(myReversedSCharArray);
System.out.println("s is: " + s);
System.out.println("myReversedS is: " + myReversedS);
int theLengthOfRS = myReversedS.length();
int[] myAsciiArrayOfReversedS = new int[theLengthOfRS];
int[] myDifferencesArrayOfReversedS = new int[theLengthOfRS - 1];
//this puts the ascii values of s-reversed's characters into the array
for(int i = 0; i < theLengthOfRS; i++){
myAsciiArrayOfReversedS[i] = (int)((char)(s.charAt(i)));
}
System.out.println("myAsciiArrayOfReversedS is: " + myAsciiArrayOfReversedS);
//this puts the differences of ascii values of s-reversed's characters into the array
for(int k = 0; k < myAsciiArrayOfReversedS.length - 1; k++){
myDifferencesArrayOfReversedS[k] = Math.abs(myAsciiArrayOfReversedS[k+1] - myAsciiArrayOfReversedS[k]);
}
System.out.println("myDifferencesArrayOfReversedS is: " + myDifferencesArrayOfReversedS);
//Now compare the 2 arrays myDifferencesArrayOfS[] and myDifferencesArrayOfReversedS[]
if(myDifferencesArrayOfS == myDifferencesArrayOfReversedS){
return "Funny";
}
else{
return "Not Funny";
}
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int qItr = 0; qItr < q; qItr++) {
String s = scanner.nextLine();
String result = funnyString(s);
bufferedWriter.write(result);
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}}