Skip to content

Commit

Permalink
Merge pull request #167 from rasik210/funnystring
Browse files Browse the repository at this point in the history
Funny string problem: C# solution
  • Loading branch information
RyanFehr authored Jun 18, 2018
2 parents 05ce847 + 4ba736b commit 7f6eaca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions Algorithms/Strings/Funny String/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Problem: https://www.hackerrank.com/challenges/funny-string/problem
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
- Here we have to iterate through the string and keep checking the difference between adjacent characters
- The only trick is that we can check the difference both in forward and reverse direction at the same time and compare them in single go. So, we don't require two iteration.
Time Complexity: O(n) //we need to iterate the entire string once.
Space Complexity: O(n) //we need to store entire string in memory to process both in forward and reverse direction at the same time.
*/
using System;
class Solution
{
static void Main(string[] args)
{
var testCount = int.Parse(Console.ReadLine());
for (var j = 0; j < testCount; j++)
{
var inputString = Console.ReadLine();
var i = 0;
for (; i < inputString.Length - 1; i++)
{
var diffForwardSeries = Math.Abs(inputString[i] - inputString[i + 1]);
var diffBackwardSeries = Math.Abs(inputString[inputString.Length - 1 - i] - inputString[inputString.Length - 1 - i - 1]);
if (diffForwardSeries != diffBackwardSeries)
break;
}

if (i == inputString.Length - 1)
Console.WriteLine("Funny");
else
Console.WriteLine("Not Funny");
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
| | [Pangrams](https://www.hackerrank.com/challenges/pangrams)| <ul><li>[Java](./Algorithms/Strings/Pangrams/Solution.java)</li><li>[C#](./Algorithms/Strings/Pangrams/Solution.cs)</li><ul> | _O(n)_ | _O(1)_ | Easy | 20| ||
| | [Weighted Uniform Strings](https://www.hackerrank.com/challenges/weighted-uniform-string)| <ul><li>[Java](./Algorithms/Strings/Weighted%20Uniform%20Strings/Solution.java)</li><li>[C#](./Algorithms/Strings/Weighted%20Uniform%20Strings/Solution.cs)</li><ul> | _O(n)_ | _O(n)_ | Easy | 20| ||
| | [Separate the Numbers](https://www.hackerrank.com/challenges/separate-the-numbers)| <ul><li>Java</li><li>[C#](./Algorithms/Strings/Separate%20the%20Numbers/Solution.cs)</li></ul> | _O(n)_ | _O(n)_ | Easy | 20| ||
| | [Funny String](https://www.hackerrank.com/challenges/funny-string)| <ul><li>[Java](./Algorithms/Strings/Funny%20String/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy | 25| ||
| | [Funny String](https://www.hackerrank.com/challenges/funny-string)| <ul><li>[Java](./Algorithms/Strings/Funny%20String/Solution.java)</li><li>[C#](./Algorithms/Strings/Funny%20String/Solution.cs)</li><ul> | _O(n)_ | _O(n)_ | Easy | 25| ||
| | [Gemstones](https://www.hackerrank.com/challenges/gem-stones)| <ul><li>[Java](./Algorithms/Strings/Gemstones/Solution.java)</li><ul> | _O(n)_ | _O(n)_ | Easy | 20| ||
| | [Alternating Characters](https://www.hackerrank.com/challenges/alternating-characters)| <ul><li>[Java](./Algorithms/Strings/Alternating%20Characters/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy | 20| ||
| | [Beautiful Binary String](https://www.hackerrank.com/challenges/beautiful-binary-string)| <ul><li>[Java](./Algorithms/Strings/Beautiful%20Binary%20String/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy | 20| ||
Expand Down

0 comments on commit 7f6eaca

Please sign in to comment.