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
.
Question from hackerrank.
Solution in C# -
class Solution
{
static string funnyString(string s) {
char[] arr = s.ToCharArray();
Array.Reverse(arr);
string s2 = new string(arr);
string ans = "";
string ans2 = "";
for(int i=0, j=0; i<s.Length-1 && j<s2.Length-1; i++,j++)
{
ans = ans + Math.Abs(s[i]-s[i+1]);
ans2 = ans2 + Math.Abs(s2[j]-s2[j+1]);
}
if(ans.Equals(ans2))
return "Funny";
else
return "Not Funny";
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int q = Convert.ToInt32(Console.ReadLine());
for (int qItr = 0; qItr < q; qItr++) {
string s = Console.ReadLine();
string result = funnyString(s);
textWriter.WriteLine(result);
}
textWriter.Flush();
textWriter.Close();
}
}
Post a Comment