Saturday, April 18, 2015

Funny Strings


Here is my Code to Check Whether the String is Funny Or Not.

Suppose you have a string S which has length N and is indexed from 0 to N1. String R is the reverse of the string S. The string S is funny if the condition |SiSi1|=|RiRi1|is true for every i from 1 to N1.


import java.util.Scanner;
public class FunnyString
{
    public static void main(String args[])
    {
        int T,i;
        Scanner sc = new Scanner(System.in);
        T = sc.nextInt(); //Number of Test Cases
        String[] S = new String[T];
        StringBuffer[] R = new StringBuffer[T];  
        if(T>=1 && T<=10)
        {
            for(i=1;i<=T;i++)
            {
                S[i-1] = sc.next();
                StringBuffer S1 = new StringBuffer(S[i-1]); 
                R[i-1] = S1.reverse();
            }
        }
        char[] ch = null;
        char[] ch1 = null;
        FAST:
        for(int z=1;z<=T;z++)
        {
            if(S[z-1].length()>=1 && S[z-1].length()<=10000)
            {
                OUTER:
                for(i=1;i<S[z-1].length();i++)
                {
                    ch = new char[S[z-1].length()];
                    ch1 = new char[S[z-1].length()];
                    ch = S[z-1].toCharArray();
                    String r = new String(R[z-1]);
                    ch1 = r.toCharArray();
                    int v = (int)(ch[i]-ch[i-1]);
                    int f = Math.abs(v);
                    int b = (int)(ch1[i]-ch1[i-1]);
                    int g = Math.abs(b);
                    if(f==g)
                    {
                        continue OUTER;
                    }
                    else
                    {
                        System.out.println("Not Funny");
                        continue FAST;
                    }
                }
                System.out.println("Funny");
                continue FAST;
            }
        }
    }
}

No comments:

Post a Comment