-
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.
Mars exploration problem:C# solution
Added a new solution for mars exploration problem using C# programming language
- Loading branch information
Showing
2 changed files
with
41 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,40 @@ | ||
/* | ||
Problem: https://www.hackerrank.com/challenges/mars-exploration/problem | ||
C# Language Version: 7.0 | ||
.Net Framework Version: 4.7 | ||
Tool Version : Visual Studio Community 2017 | ||
Thoughts : | ||
- Pick 3 characters at a time from the string and check whether they match with SOS string or not. | ||
- For each unmatched character increment a counter. | ||
- Print the counter once entire string has been traversed. | ||
Time Complexity: O(n) //entire string traversal is required once. | ||
Space Complexity: O(1) //number of dynamically allocated variables remain constant for any input. | ||
*/ | ||
|
||
using System; | ||
|
||
class Solution | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var alteredCharCount = 0; | ||
var sosSignal = "SOS"; | ||
var index = 0; | ||
var nextChar = Console.Read(); | ||
//special handling for hacker rank execution environment | ||
//while running on my own computer I compare it with ascii code of enter key which is 13. | ||
while (nextChar != -1) | ||
{ | ||
if ((char)nextChar != sosSignal[index++]) | ||
alteredCharCount++; | ||
|
||
if (index % 3 == 0) | ||
index = 0; | ||
nextChar = Console.Read(); | ||
} | ||
|
||
Console.WriteLine(alteredCharCount); | ||
} | ||
} |
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