-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms hash table): ransom note #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Ransom Note | ||
|
|
||
| Given two strings, ransom_note and magazine, check if ransom_note can be constructed using the letters from magazine. | ||
| Return TRUE if it can be constructed, FALSE otherwise. | ||
|
|
||
| > Note: A ransom note is a written message that can be constructed by using the letters available in the given magazine. | ||
| > The magazine can have multiple instances of the same letter. Each instance of the letter in the magazine can only be | ||
| > used once to construct the ransom note. | ||
|
|
||
| ## Constraints | ||
|
|
||
| - 1 <= `ransom_note.length`, `magazine.length` <= 10^3 | ||
| - The `ransom_note` and `magazine` consist of lowercase English letters | ||
|
|
||
| ## Examples | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
| ## Topics | ||
|
|
||
| - Hash Table | ||
| - String | ||
| - Counting | ||
|
|
||
| ## Solution | ||
| An optimized approach to solve this problem is to keep track of the occurrences of characters using the hash map. We | ||
| store the frequency of each character of the magazine in the hash map. After storing the frequencies, we iterate over | ||
| each character in the ransom note and check if the character is present in the hash map and its frequency is greater | ||
| than zero. If it is, we decrement the frequency by 1, indicating that we’ve used that character to construct the ransom | ||
| note. If the character is not present in the hash map or its frequency is 0, we immediately return FALSE since it's | ||
| impossible to construct the ransom note. | ||
|
|
||
| If we successfully iterate through all characters in the ransom note without encountering a character that is not present | ||
| in the hash map or its frequency is 0, we return TRUE, indicating that we can construct the ransom note from the | ||
| characters available in the magazine. | ||
|
|
||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
| ### Summary | ||
|
|
||
| - Create a hash map to store the frequencies of each character in the magazine. | ||
| - Iterate through each character in the ransom note and check the following conditions: | ||
| - If the character is not in the hash map or the frequency of the character is 0, return FALSE | ||
| - Otherwise, decrement the frequency of the character in the hash map by 1. | ||
| - Return TRUE if we successfully iterate through all characters in the ransom note. | ||
|
|
||
| ### Time Complexity | ||
|
|
||
| The time complexity of this solution is O(n+m), where n is the length of the ransom note and m is the length of the | ||
| magazine. | ||
|
|
||
| ### Space Complexity | ||
|
|
||
| The space complexity of this solution is O(1) because we have a constant number of lowercase English letters | ||
| (26 unique characters). Therefore, the space required by the hash map will remain constant. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| from collections import Counter | ||
|
|
||
|
|
||
| def can_construct(ransom_note: str, magazine: str) -> bool: | ||
| """ | ||
| Checks if it is possible to construct a ransom note from the given letters in the magazine where each letter in the | ||
| magazine can only be used once. If a letter in the magazine occurs more than once, it can only be used that many | ||
| number of times, so `magazine=aabc`, means we can use letter a twice, but not more than that. | ||
|
|
||
| Complexity Analysis: | ||
|
|
||
| Time O(n + m): Where n is the number of letters in `ransom_note` and `m` is the number of letters in `magazine`. This | ||
| is because we iterate through the magazine to count occurrences of the number of letters and again through ransom_note | ||
| to check if each letter is present in the magazine | ||
|
|
||
| Space O(1): Since there are only English letters which are 26, the space used by the hash table is always count going | ||
| to be constant. | ||
|
|
||
| Args: | ||
| ransom_note(str): the string with which we intend to construct from the magazine. | ||
| magazine(str): the string with which we intend to use to construct ransom_note | ||
| Returns: | ||
| bool: True if we can construct ransom note from the magazine, false otherwise. | ||
| """ | ||
| # No need to proceed if we don't have a magazine to construct a ransom note from | ||
| if not magazine: | ||
| return False | ||
|
|
||
| # Count the number of occurrences of each letter in the magazine. This will be used to keep track of the number of | ||
| # letters we can use when constructing the ransom note | ||
| occurrences = Counter(magazine) | ||
|
|
||
| # Iterate through each letter in the ransom note to check if it is in the magazine | ||
| for letter in ransom_note: | ||
| # If a letter does not exist in the frequency map of the magazine or the count has now become 0 meaning we can't | ||
| # use the letter from the magazine, then there is no need to proceed with the iteration, we can't construct | ||
| # the ransom note | ||
| if letter not in occurrences or occurrences[letter] == 0: | ||
| return False | ||
|
|
||
| # if the letter is in the occurrences, we decrease the count of the occurrences of the letter and the number | ||
| # of letters left to construct the ransom note | ||
| occurrences[letter] -= 1 | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def can_construct_2(ransom_note: str, magazine: str) -> bool: | ||
| # create an empty hash map to store the frequency of each character in the magazine string | ||
| frequency = {} | ||
|
|
||
| for char in magazine: | ||
| # if character is already present in hash map then increment | ||
| # its frequency by 1 | ||
| if char in frequency: | ||
| frequency[char] += 1 | ||
|
|
||
| # else count its first occurrence | ||
| else: | ||
| frequency[char] = 1 | ||
|
|
||
| for char in ransom_note: | ||
| # if the character is not in the hash map or its count is 0, return False | ||
| if char not in frequency or frequency[char] == 0: | ||
| return False | ||
|
|
||
| # otherwise, decrease the character's frequency in the hash map by 1 | ||
| else: | ||
| frequency[char] -= 1 | ||
|
|
||
| return True |
Binary file added
BIN
+18.9 KB
algorithms/hash_table/ransom_note/images/examples/ransom_note_example_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.7 KB
algorithms/hash_table/ransom_note/images/examples/ransom_note_example_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+16.7 KB
algorithms/hash_table/ransom_note/images/examples/ransom_note_example_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+17.1 KB
algorithms/hash_table/ransom_note/images/examples/ransom_note_example_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24.8 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.4 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+30.9 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+36.3 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.1 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.4 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+28.3 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.3 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.2 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.8 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+27.9 KB
algorithms/hash_table/ransom_note/images/solutions/ransom_note_solution_9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import unittest | ||
| from parameterized import parameterized | ||
| from algorithms.hash_table.ransom_note import can_construct, can_construct_2 | ||
|
|
||
| RANSOM_NOTE_TEST_CASES = [ | ||
| ( | ||
| "long_note_success", | ||
| "codinginterviewquestions", | ||
| "aboincsdefoetingvqtniewonoregessnutins", | ||
| True, | ||
| ), | ||
| ("missing_letter", "code", "coingd", False), | ||
| ("shuffled_letters", "codinginterview", "vieewidingcodinter", True), | ||
| ("subset_of_magazine", "program", "programming", True), | ||
| ("repeated_letters", "me", "meme", True), | ||
| ("single_char_mismatch", "a", "b", False), | ||
| ("insufficient_repeated_char", "aa", "ab", False), | ||
| ("sufficient_repeated_char", "aa", "aab", True), | ||
| ("empty_ransom_note", "", "abc", True), | ||
| ("empty_magazine", "a", "", False), | ||
| ] | ||
|
|
||
|
|
||
| class RansomNoteTestCase(unittest.TestCase): | ||
| @parameterized.expand(RANSOM_NOTE_TEST_CASES) | ||
| def test_can_construct(self, _, ransom_note: str, magazine: str, expected: bool): | ||
| actual = can_construct(ransom_note, magazine) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| @parameterized.expand(RANSOM_NOTE_TEST_CASES) | ||
| def test_can_construct_2(self, _, ransom_note: str, magazine: str, expected: bool): | ||
| actual = can_construct_2(ransom_note, magazine) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.