From d5c583669480192e46209c8c52885e70e761c678 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 21 Jun 2024 20:00:15 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Dictionaries=20and=20Hashmaps:=20Hash=20Tables:=20Two=20Str?= =?UTF-8?q?ings.=20Solved=20=E2=9C=93.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TwoStrings.Test.cs | 49 ++++++++++++ .../dictionaries_and_hashmaps/TwoStrings.cs | 32 ++++++++ .../dictionaries_and_hashmaps/two-strings.md | 80 +++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.Test.cs create mode 100644 algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.cs create mode 100644 docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md diff --git a/algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.Test.cs b/algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.Test.cs new file mode 100644 index 0000000..5f653e4 --- /dev/null +++ b/algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.Test.cs @@ -0,0 +1,49 @@ +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; + +[TestClass] +public class TwoStringsTest +{ + public class TwoStringsTestCase + { + public string title = ""; + public string s1 = ""; + public string s2 = ""; + public string expected = "Yes"; + } + + private static readonly TwoStringsTestCase[] tests = [ + new() + { + title = "Example 1", + s1 = "and", + s2 = "art", + expected = "Yes" + }, + new() + { + title = "Example 2", + s1 = "be", + s2 = "cat", + expected = "No" + }, + new() + { + title = "Sample Test Case 0", + s1 = "hello", + s2 = "world", + expected = "Yes" + }, + ]; + + [TestMethod] + public void testTwoStrings() + { + string result; + + foreach (TwoStringsTestCase test in tests) + { + result = TwoStrings.twoStrings(test.s1, test.s2); + Assert.AreEqual(test.expected, result); + } + } +} diff --git a/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.cs b/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.cs new file mode 100644 index 0000000..43747f9 --- /dev/null +++ b/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/TwoStrings.cs @@ -0,0 +1,32 @@ +// @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md]] + +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; + +using System.Diagnostics.CodeAnalysis; + +public class TwoStrings +{ + [ExcludeFromCodeCoverage] + protected TwoStrings() { } + + private static readonly string __YES__ = "Yes"; + private static readonly string __NO__ = "No"; + private static readonly char __EMPTY_CHAR__ = '\0'; + + public static bool twoStringsCompute(string s1, string s2) + { + char occurrence = s1.FirstOrDefault(c => s2.Contains(c), __EMPTY_CHAR__); + + if (occurrence != __EMPTY_CHAR__) + { + return true; + } + + return false; + } + + public static string twoStrings(string s1, string s2) + { + return twoStringsCompute(s1, s2) ? __YES__ : __NO__; + } +} diff --git a/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md b/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md new file mode 100644 index 0000000..3588e24 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two-strings.md @@ -0,0 +1,80 @@ +# [Two Strings](https://www.hackerrank.com/challenges/two-strings) + +- Difficulty: `#easy` +- Category: `#ProblemSolvingBasic` + +Given two strings, determine if they share a common substring. +A substring may be as small as one character. + +## Example + +`s1 = 'and'` +`s1 = 'art'` + +These share the common substring `a`. + +`s1 = 'be'` +`s1 = 'cat'` + +These do not share a substring. + +## Function Description + +Complete the function twoStrings in the editor below. + +twoStrings has the following parameter(s): + +- `string s1`: a string +- `string s2`: another string + +## Returns + +- `string`: either YES or NO + +## Input Format + +The first line contains a single integer , the number of test cases. + +The following pairs of lines are as follows: + +The first line contains string `s1`. +The second line contains string `s2`. + +## Constraints + +- `s1` and `s2` consist of characters in the range ascii[a-z]. +- $ 1 \leq p \leq 10 $ +- $ 1 \leq |s1|, |s2| \leq 10^5 $ + +## Output Format + +For each pair of strings, return `YES` or `NO`. + +## Sample Input + +```text +2 +hello +world +hi +world +``` + +## Sample Output + +```text +YES +NO +``` + +## Explanation + +We have pairs to check: + +1. `s1 = "hello"`, `s2 = "world"`. The substrings `"o"` and `"l"` +are common to both strings. +2. `a = "hi"`, `b = "world"`. `s1` and `s2` share no common substrings. + +## Appendix + +[Solution notes](two-strings-solution-notes.md)