Created
March 13, 2016 02:02
-
-
Save jianminchen/e577e9180305f0343fd5 to your computer and use it in GitHub Desktop.
HackerRank - Funny String https://www.hackerrank.com/challenges/funny-string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FunnyString | |
{ | |
/* | |
* Funny String | |
* https://www.hackerrank.com/challenges/funny-string | |
* Test Case 0: S0= "acxz" | |
|c−a|=2=|x−z| | |
|x−c|=21=|c−x| | |
|z−x|=2=|a−c| | |
We print Funny. | |
* | |
* | |
* | |
*/ | |
class Solution | |
{ | |
static void Main(string[] args) | |
{ | |
string s1 = Console.ReadLine(); | |
int n = Convert.ToInt16(s1); | |
for(int i=0;i<n;i++) | |
{ | |
string s = Console.ReadLine(); | |
string output = "Not Funny"; | |
if (isFunnyString(s)) | |
output = "Funny"; | |
Console.WriteLine(output); | |
} | |
} | |
/* | |
* string length >=2 | |
* | |
*/ | |
public static bool isFunnyString(string s) | |
{ | |
if (s == null || s.Length == 0) | |
return false; | |
int l = s.Length; | |
for(int i=0; i<l/2; i++) | |
{ | |
if (calValue(s, i, true) != calValue(s, i, false)) | |
return false; | |
} | |
return true; | |
} | |
/* | |
* Forward, i, i+1 | |
* Backward, i, i-1 | |
*/ | |
private static int calValue(string s, int i, bool forward) | |
{ | |
int l = s.Length; | |
return forward ? (Math.Abs(s[i] - s[i + 1])) : (Math.Abs(s[l - 1 - i] - s[l - 2 - i])); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment