HackerRank Challenge 4
//Funny String
//==================================
/*
* Suppose you have a String,S , of length N that is indexed from 0 to N-1 . You also have some String, R , that is the reverse of String S. S is funny if the condition S[i]-S[i-1]=R[i]-R[i-1] is true for every character i from 0 to N-1 .
*
*/
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
string[] str= new string[n];
for(int i=0;i<n;i++)
{
str[i]= Console.ReadLine();
}
for(int i=0 ;i<n;i++)
{
char[] arr= str[i].ToCharArray();
char[] rev_arr=new char[arr.Length];
for(int j=0;j<arr.Length;j++)
{
rev_arr[j]=arr[arr.Length-1 - j];
}
string status = "Funny";
int count = 0;
for(int k=0;k<arr.Length -1;k++)
{
int diff1=Math.Abs(arr[k+1] - arr[k]);
int diff2=Math.Abs(rev_arr[k+1] - rev_arr[k]);
if (diff1 == diff2)
count = count + 0;
else
count = count + 1;
}
if (count == 0)
Console.WriteLine(status);
else
Console.WriteLine("Not Funny");
}
//==================================
/*
* Suppose you have a String,S , of length N that is indexed from 0 to N-1 . You also have some String, R , that is the reverse of String S. S is funny if the condition S[i]-S[i-1]=R[i]-R[i-1] is true for every character i from 0 to N-1 .
*
*/
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
string[] str= new string[n];
for(int i=0;i<n;i++)
{
str[i]= Console.ReadLine();
}
for(int i=0 ;i<n;i++)
{
char[] arr= str[i].ToCharArray();
char[] rev_arr=new char[arr.Length];
for(int j=0;j<arr.Length;j++)
{
rev_arr[j]=arr[arr.Length-1 - j];
}
string status = "Funny";
int count = 0;
for(int k=0;k<arr.Length -1;k++)
{
int diff1=Math.Abs(arr[k+1] - arr[k]);
int diff2=Math.Abs(rev_arr[k+1] - rev_arr[k]);
if (diff1 == diff2)
count = count + 0;
else
count = count + 1;
}
if (count == 0)
Console.WriteLine(status);
else
Console.WriteLine("Not Funny");
}
Comments
Post a Comment