Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HackerRank in a String! problem: C# solution #157

Merged
merged 2 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Algorithms/Strings/HackerRank in a String!/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Problem: https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
- iterate through the entire input string character by character
- keep a track of which characters have been already found so far from "hackerrank" in the input string. e.g. let's say if "hack" has been already found then look for 'e' continuing with the iteration.

Time Complexity: O(n) //we have to iterate the entire input string.
Space Complexity: O(1) //number of dynamically allocated variables remain constant for any input.


*/

using System;

class Solution
{
static void Main(string[] args)
{
var queryCount = int.Parse(Console.ReadLine());

for (var i = 0; i < queryCount; i++)
{
var nextIndex = 0;
var searchPattern = "hackerrank";

var nextChar = Console.Read();
//special handling for hacker rank execution environment. I went crazy to figure out this condition
//hacker rank uses \n line feed character (ASCII code 10) for changing line for next test case and -1 as end of file which marks the end of last test case
//In Windows it is \r\n so while running on my computer I compare it against 13 only.

while (nextChar != 10 && nextChar != -1)
{
if (nextIndex < 10 && (char)nextChar == searchPattern[nextIndex])
nextIndex++;

nextChar = Console.Read();
}
if (nextIndex == 10)
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
| | [Caesar Cipher](https://www.hackerrank.com/challenges/caesar-cipher-1)| <ul><li>[Java](./Algorithms/Strings/Caesar%20Cipher/Solution.java)</li><li>[C#](./Algorithms/Strings/Caesar%20Cipher/Solution.cs)</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><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| ||
| | [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><li>[C#](./Algorithms/Strings/HackerRank%20in%20a20String!/Solution.cs)</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| ||
| | [Separate the Numbers](https://www.hackerrank.com/challenges/separate-the-numbers)| <ul><li>Java</li></ul> | | | Easy | 20| ||
Expand Down