From b9c21b7f881de02ebc8bcaac600543536b65547a Mon Sep 17 00:00:00 2001 From: Rasik Bihari Tiwari Date: Thu, 24 May 2018 15:15:14 +0530 Subject: [PATCH] HackerRank in a String! problem: C# solution Added a new solution for HackerRank in a String! problem using C# programming language. --- .../HackerRank in a String!/Solution.cs | 47 +++++++++++++++++++ README.md | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 Algorithms/Strings/HackerRank in a String!/Solution.cs diff --git a/Algorithms/Strings/HackerRank in a String!/Solution.cs b/Algorithms/Strings/HackerRank in a String!/Solution.cs new file mode 100644 index 0000000..ab9f83b --- /dev/null +++ b/Algorithms/Strings/HackerRank in a String!/Solution.cs @@ -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"); + } + } +} diff --git a/README.md b/README.md index 92e9b08..fcf6af2 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ | | [Caesar Cipher](https://www.hackerrank.com/challenges/caesar-cipher-1)|