From 2ca0becbcf1d8ef53104e20ea9a8bbca10ab8325 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 21 Jun 2024 12:01:34 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Dictionaries=20and=20Hashmaps:=20Hash=20Tables:=20Ransom=20?= =?UTF-8?q?Note.=20Solved=20=E2=9C=93.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RansomNote.Test.cs | 54 ++++++++++ .../dictionaries_and_hashmaps/RansomNote.cs | 51 +++++++++ .../ctci-ransom-note.md | 102 ++++++++++++++++++ 3 files changed, 207 insertions(+) create mode 100644 algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.Test.cs create mode 100644 algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs create mode 100644 docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md diff --git a/algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.Test.cs b/algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.Test.cs new file mode 100644 index 0000000..fd6f193 --- /dev/null +++ b/algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.Test.cs @@ -0,0 +1,54 @@ +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; + +[TestClass] +public class RansomNoteTest +{ + public class RansomNoteTestCase + { + public string title = ""; + public List magazine = []; + public List note = []; + public string expected = ""; + } + + public class ArraysLeftRotationsTestCase + { + public List input = []; public int d; public List expected = []; + } + + private static readonly RansomNoteTestCase[] tests = [ + new() + { + title = "Sample Test Case 0", + magazine = ["give", "me", "one", "grand", "today", "night"], + note = ["give", "one", "grand", "today"], + expected = "Yes" + }, + new() + { + title = "Sample Test Case 1", + magazine = ["two", "times", "three", "is", "not", "four"], + note = ["two", "times", "two", "is", "four"], + expected = "No" + }, + new() + { + title = "Sample Test", + magazine = ["two", "two", "times", "three", "is", "not", "four"], + note = ["two", "times", "two", "is", "four"], + expected = "Yes" + }, + ]; + + [TestMethod] + public void testCheckMagazine() + { + string result; + + foreach (RansomNoteTestCase test in tests) + { + result = RansomNote.checkMagazine(test.magazine, test.note); + Assert.AreEqual(test.expected, result); + } + } +} diff --git a/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs b/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs new file mode 100644 index 0000000..de91eac --- /dev/null +++ b/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/RansomNote.cs @@ -0,0 +1,51 @@ +// @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; +using System.Collections.Generic; + +public class RansomNote +{ + [ExcludeFromCodeCoverage] + protected RansomNote() { } + + private static readonly string __YES__ = "Yes"; + private static readonly string __NO__ = "No"; + + public static bool checkMagazineCompute(List magazine, List note) + { + Dictionary dictionary = []; + + foreach (string word in magazine) + { + if (dictionary.TryGetValue(word, out int? value)) + { + dictionary[word] += 1; + } + else + { + dictionary[word] = 1; + } + } + + foreach (string word in note) + { + if (dictionary.TryGetValue(word, out int? value) && value > 0) + { + dictionary[word] -= 1; + } + else + { + return false; + } + } + + return true; + } + + public static string checkMagazine(List magazine, List note) + { + return checkMagazineCompute(magazine, note) ? __YES__ : __NO__; + } +} diff --git a/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md b/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md new file mode 100644 index 0000000..1e9df62 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/ctci-ransom-note.md @@ -0,0 +1,102 @@ +# [Hash Tables: Ransom Notes](https://www.hackerrank.com/challenges/ctci-ransom-note) + +- Difficulty: `#easy` +- Category: `#ProblemSolvingIntermediate` + +Harold is a kidnapper who wrote a ransom note, but now he is worried it will be +traced back to him through his handwriting. He found a magazine and wants to +know if he can cut out whole words from it and use them to create an untraceable +replica of his ransom note. +The words in his note are case-sensitive and he must use only whole words +available in the magazine. +He cannot use substrings or concatenation to create the words he needs. + +Given the words in the magazine and the words in the ransom note, +print `Yes` if he can replicate his ransom note exactly using whole words +from the magazine; otherwise, print `No`. + +## Example + +`magazine` = "attack at dawn" `note` = "Attack at dawn" + +The magazine has all the right words, but there is a case mismatch. +The answer is `No`. + +## Function Description + +Complete the checkMagazine function in the editor below. +It must print `Yes` if the note can be formed using the magazine, or . + +checkMagazine has the following parameters: + +- `string magazine[m]`: the words in the magazine +- `string note[n]`: the words in the ransom note + +## Prints + +- string: either or , no return value is expected + +## Input Format + +The first line contains two space-separated integers, `m` and `n`, +the numbers of words in the and the , respectively. + +The second line contains `m` space-separated strings, each `magazine[i]`. + +The third line contains `n` space-separated strings, each `node[i]`. + +## Constraints + +- $ 1 \leq m, n \leq 30000 $ +- $ 1 \leq $ length of `magazine[i]` and `note[i]` $ \leq 5 $ +- Each word consists of English alphabetic letters (i.e., `a` to `z` and `A` to `Z`). + +## Sample Input 0 + +```text +6 4 +give me one grand today night +give one grand today +``` + +## Sample Output 0 + +```text +Yes +``` + +## Sample Input 1 + +```text +6 5 +two times three is not four +two times two is four +``` + +## Sample Output 1 + +```text +No +``` + +## Explanation 1 + +'two' only occurs once in the magazine. + +## Sample Input 2 + +```text +7 4 +ive got a lovely bunch of coconuts +ive got some coconuts +``` + +## Sample Output 2 + +```text +No +``` + +## Explanation 2 + +Harold's magazine is missing the word `some`.