-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alternating characters problem:C# solution
Added a new solution for alternating characters problem using C# programming language
- Loading branch information
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
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
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); | ||
} | ||
} | ||
} |
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