Thursday, 7 December 2017

Funny String Hacker Rank Problem Solution Using JAVA.

Suppose you have a String, , of length  that is indexed from  to . You also have some String, , that is the reverse of String  is funny if the condition  is true for every character from  to .
Note: For some String  denotes the ASCII value of the  -indexed character in . The absolute value of an integer, , is written as .
Input Format
The first line contains an integer,  (the number of test cases). 
Each line  of the  subsequent lines contain a string, .
Constraints
Output Format
For each String  (where ), print whether it is  or  on a new line.
Sample Input
2
acxz
bcxz
Sample Output
Funny
Not Funny
Explanation
Test Case 0 
 
 
 
As each comparison is equal, we print .
Test Case 1 
, but  
At this point, we stop evaluating  (as ) and print .
Java Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String funnyString(String s){
        // Complete this function
        
        int count1 =0;
        int count2 =0;
        int j=0;
        for(int i=0;i<s.length()-1;i++)
        {
            count1 = count1+Math.abs((int)s.charAt(i)-(int)s.charAt(i+1));
            count2 = count2+Math.abs((int)s.charAt(s.length()-i-1)-(int)s.charAt(s.length()-i-2));
          if(count1 != count2)
          {
              j++;
              break;
          }        
        }
          if(j>0)
            return "Not Funny";
        else
            return "Funny";
       
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            String s = in.next();
            String result = funnyString(s);
            System.out.println(result);
        }
    }
}

No comments:

Post a Comment

LCM of n numbers Using Java

Found it in Geeks For Geeks : https://www.geeksforgeeks.org/lcm-of-given-array-elements/ A good Solution to be Considered: // Java Pro...