Skip to content

Commit

Permalink
Mars exploration problem:C# solution
Browse files Browse the repository at this point in the history
Added a new solution for mars exploration problem using C# programming language
  • Loading branch information
rasik210 committed May 23, 2018
1 parent 513f785 commit a660c52
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions Algorithms/Strings/Mars Exploration/Solution.cs
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);
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
| | [Two Characters](https://www.hackerrank.com/challenges/two-characters)| <ul><li>[Java](./Algorithms/Strings/Two%20Characters/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy| 15| ||
| | [Caesar Cipher](https://www.hackerrank.com/challenges/caesar-cipher-1)| <ul><li>[Java](./Algorithms/Strings/Caesar%20Cipher/Solution.java)</li><ul> | _O(n)_ | _O(n)_ | Easy | 15| ||
| | [Caesar Cipher: Encryption](https://www.hackerrank.com/challenges/linkedin-practice-caesar-cipher)| <ul><li>[Java](./Algorithms/Strings/Caesar%20Cipher%20Encryption/Solution.java)</li><ul> | _O(n)_ | _O(n)_ | Easy | 40 | ||
| | [Mars Exploration](https://www.hackerrank.com/challenges/mars-exploration)| <ul><li>[Java](./Algorithms/Strings/Mars%20Exploration/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy | 15| ||
| | [Mars Exploration](https://www.hackerrank.com/challenges/mars-exploration)| <ul><li>[Java](./Algorithms/Strings/Mars%20Exploration/Solution.java)</li><li>[C#](./Algorithms/Strings/Mars%20Exploration/Solution.cs)</li><ul> | _O(n)_ | _O(1)_ | Easy | 15| ||
| | [HackerRank in a String!](https://www.hackerrank.com/challenges/hackerrank-in-a-string)| <ul><li>[Java](./Algorithms/Strings/HackerRank%20in%20a20String!/Solution.java)</li> <li>[JS](./Algorithms/Strings/HackerRank%20in%20a20String!/Solution.js)</li><ul> | _O(n)_ | _O(1)_ | Easy | 20| ||
| | [Pangrams](https://www.hackerrank.com/challenges/pangrams)| <ul><li>[Java](./Algorithms/Strings/Pangrams/Solution.java)</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><ul> | _O(n)_ | _O(n)_ | Easy | 20| ||
Expand Down

0 comments on commit a660c52

Please sign in to comment.