Skip to content

Commit

Permalink
Merge pull request #179 from rasik210/twostrings
Browse files Browse the repository at this point in the history
 Two strings problem:C# solution
  • Loading branch information
RyanFehr authored Jul 5, 2018
2 parents 215c150 + 3da3937 commit 2e47900
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
56 changes: 56 additions & 0 deletions Algorithms/Strings/Two Strings/Solution.cs
Original file line number Diff line number Diff line change
@@ -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:
<None>
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<int>();
//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--;
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
| | [Anagram](https://www.hackerrank.com/challenges/anagram)| <ul><li>[Java](./Algorithms/Strings/Anagram/Solution.java)</li><ul> | _O(n)_ | _O(1)_ | Easy | 25| ||
| | [Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams)| <ul><li>[Java](./Algorithms/Strings/Making%20Anagrams/Solution.java)</li><ul> | _O(n)_ | _O(n)_ | Easy | 30| ||
| | [Game of Thrones - I](https://www.hackerrank.com/challenges/game-of-thrones)| <ul><li>[Java](./Algorithms/Strings/Game%20of%20Thrones-I/Solution.java)</li><li>[C#](./Algorithms/Strings/Game%20of%20Thrones-I/Solution.cs)</li><ul> | _O(n)_ | _O(1)_ | Easy | 30| ||
| | [Two Strings](https://www.hackerrank.com/challenges/two-strings)| <ul><li>[Java](./Algorithms/Strings/Two%20Strings/Solution.java)</li><ul> | _O(&#124;a&#124; + &#124;b&#124;)_ | _O(1)_ | Easy | 25| ||
| | [Two Strings](https://www.hackerrank.com/challenges/two-strings)| <ul><li>[Java](./Algorithms/Strings/Two%20Strings/Solution.java)</li><li>[C#](./Algorithms/Strings/Two%20Strings/Solution.cs)</li><ul> | _O(&#124;a&#124; + &#124;b&#124;)_ | _O(1)_ | Easy | 25| a and b are lengths of the input strings ||
| | [String Construction](https://www.hackerrank.com/challenges/string-construction)| <ul><li>[Java](./Algorithms/Strings/String%20Construction/Solution.java)</li><ul> | _O(n)_ | _O(n)_ | Easy | 25| ||
| | [Sherlock and Valid String](https://www.hackerrank.com/challenges/sherlock-and-valid-string)| <ul><li>[Java](./Algorithms/Strings/Sherlock%20and%20Valid%20String/Solution.java)</li><ul> | _O(n)_ | _O(n)_ | Hard | 100| ||
| | [Richie Rich](https://www.hackerrank.com/challenges/richie-rich)| <ul><li>Java</li> <li>[Python3](./Algorithms/Strings/Richie%20Rich/solution.py)</li><ul> | _O(n)_ | _O(n)_ | Medium | 30| ||
Expand Down

0 comments on commit 2e47900

Please sign in to comment.