From a7313f575fabb8055d5feb911611fa9b6d7a37fb Mon Sep 17 00:00:00 2001 From: Rasik Bihari Date: Sat, 9 Jun 2018 05:57:29 +0530 Subject: [PATCH] Two strings problem:C# solution Added a new solution for two strings problem using C# programming language --- Algorithms/Strings/Two Strings/Solution.cs | 56 ++++++++++++++++++++++ README.md | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Algorithms/Strings/Two Strings/Solution.cs diff --git a/Algorithms/Strings/Two Strings/Solution.cs b/Algorithms/Strings/Two Strings/Solution.cs new file mode 100644 index 0000000..df3393e --- /dev/null +++ b/Algorithms/Strings/Two Strings/Solution.cs @@ -0,0 +1,56 @@ +/* + Problem: https://www.hackerrank.com/challenges/two-strings/problem + C# Language Version: 6.0 + .Net Framework Version: 4.7 + Tool Version : Visual Studio Community 2017 + Thoughts (Key points in algorithm): + - It is about finding just one character in first string which will also be present in second string. + - Iterate the first string and create a set (hash set for O(1) ammortized searching) of all the english alphabet characters appearing in it. + - Then, iterate the second string and check if any of the characters appearing is present in the character set of first string. + - If found print YES else print NO. + + Gotchas: + + + Time Complexity: O(m + n) //m and n are length of the input strings respectively. + Space Complexity: O(1) + + */ +using System.Collections.Generic; +using System; + +class Solution { + static void Main(string[] args) { + var testcaseCount = int.Parse(Console.ReadLine()); + while (testcaseCount > 0) + { + var text1nextChar = Console.Read(); + var charMap = new HashSet(); + //special handling for hacker rank execution environment. + //for line break they use '\n' whose ascii code is 10 + //on my local box I use 13 which is ascii code for '\r' + while (text1nextChar != 10) + { + + if (!charMap.Contains(text1nextChar)) + charMap.Add(text1nextChar); + + text1nextChar = Console.Read(); + } + + var charFound = "NO"; + var text2nextChar = Console.Read(); + //special handling for hacker rank execution environment. + //for end of file they use -1 which is the end of second string of last test case. + while (text2nextChar != 10 && text2nextChar != -1) + { + if (charMap.Contains(text2nextChar) && charFound != "YES") + charFound = "YES"; + text2nextChar = Console.Read(); + } + + Console.WriteLine(charFound); + testcaseCount--; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index a87a2a3..bbde098 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ | | [Anagram](https://www.hackerrank.com/challenges/anagram)|
  • [Java](./Algorithms/Strings/Anagram/Solution.java)
    • | _O(n)_ | _O(1)_ | Easy | 25| || | | [Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams)|
      • [Java](./Algorithms/Strings/Making%20Anagrams/Solution.java)
        • | _O(n)_ | _O(n)_ | Easy | 30| || | | [Game of Thrones - I](https://www.hackerrank.com/challenges/game-of-thrones)|
          • [Java](./Algorithms/Strings/Game%20of%20Thrones-I/Solution.java)
            • | _O(n)_ | _O(1)_ | Easy | 30| || -| | [Two Strings](https://www.hackerrank.com/challenges/two-strings)|
              • [Java](./Algorithms/Strings/Two%20Strings/Solution.java)
                • | _O(|a| + |b|)_ | _O(1)_ | Easy | 25| || +| | [Two Strings](https://www.hackerrank.com/challenges/two-strings)|
                  • [Java](./Algorithms/Strings/Two%20Strings/Solution.java)
                  • [C#](./Algorithms/Strings/Two%20Strings/Solution.cs)
                    • | _O(|a| + |b|)_ | _O(1)_ | Easy | 25| a and b are lengths of the input strings || | | [String Construction](https://www.hackerrank.com/challenges/string-construction)|
                      • [Java](./Algorithms/Strings/String%20Construction/Solution.java)
                        • | _O(n)_ | _O(n)_ | Easy | 25| || | | [Sherlock and Valid String](https://www.hackerrank.com/challenges/sherlock-and-valid-string)|
                          • [Java](./Algorithms/Strings/Sherlock%20and%20Valid%20String/Solution.java)
                            • | _O(n)_ | _O(n)_ | Hard | 100| || | | [Richie Rich](https://www.hackerrank.com/challenges/richie-rich)|
                              • Java
                              • [Python3](./Algorithms/Strings/Richie%20Rich/solution.py)
                                • | _O(n)_ | _O(n)_ | Medium | 30| ||