Skip to content

Commit

Permalink
Alternating characters problem:C# solution
Browse files Browse the repository at this point in the history
Added a new solution for alternating characters problem using C# programming language
  • Loading branch information
rasik210 committed Jun 1, 2018
1 parent 9934fb8 commit 4de2188
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 47 additions & 0 deletions Algorithms/Strings/Alternating Characters/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Problem: https://www.hackerrank.com/challenges/alternating-characters/problem
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts (Key points in algorithm):
- We've to simply track the alternating characters while traversing the characters of the string
- If the character being traversed is same as previous character then we need to delete it.
- For delete operation don't do physical deletion of character from the string. Instead, keep a counter, increment it and move to the next character.
- Print the counter value in the end.
Gotchas:
<None>
Time Complexity: O(n) //we need to traverse the entire string
Space Complexity: O(1) //we are reading every string input in a test case from console input stream character by character
in place of saving the entire string in memory. So constant space.
*/
using System;
class Solution
{
static void Main(string[] args)
{
var testCaseCount = int.Parse(Console.ReadLine());
for (var i = 0; i < testCaseCount; i++)
{
var deletedCharactercount = 0;
var nextChar = Console.Read();
var lastChar = nextChar;

nextChar = Console.Read();
//special handling for hacker rank execution environment. 10 is ascii code of line feed '\n'.
//Hacker rank uses '\n' character for end of a test case and -1 for end of file which marks the end of last test case.
//while running on my own computer I compare it with ascii code of carriage return '\r' which is 13.
while (nextChar != 10 && nextChar != -1)
{
if (nextChar == lastChar)
deletedCharactercount++;
else
lastChar = nextChar;

nextChar = Console.Read();
}
Console.WriteLine(deletedCharactercount);
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
| | [Separate the Numbers](https://www.hackerrank.com/challenges/separate-the-numbers)| <ul><li>Java</li></ul> | | | 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| ||
| | [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| ||
| | [Alternating Characters](https://www.hackerrank.com/challenges/alternating-characters)| <ul><li>[Java](./Algorithms/Strings/Alternating%20Characters/Solution.java)</li><li>[C#](./Algorithms/Strings/Alternating%20Characters/Solution.cs)</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| ||
| | [The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery)| <ul><li>[Java](./Algorithms/Strings/The%20Love-Letter%20Mystery/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy | 20| ||
| | [Determining DNA Health](https://www.hackerrank.com/challenges/determining-dna-health)| <ul><li>Java</li></ul> | | | Hard | 50| ||
Expand Down

0 comments on commit 4de2188

Please sign in to comment.