-
Notifications
You must be signed in to change notification settings - Fork 2
feat(datastructures, hashmap): design a hashmap #182
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f09d2cc
feat(datastructures, hashmap): design a hashmap)
BrianLusina 78a2a3f
updating DIRECTORY.md
c8b6ea4
Update algorithms/hash_table/jewels_and_stones/__init__.py
BrianLusina 096619c
Update datastructures/hashmap/hash_map.py
BrianLusina 9d54461
Update datastructures/hashmap/README.md
BrianLusina 3095a43
Update DIRECTORY.md
BrianLusina 060ad3c
updating DIRECTORY.md
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
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,61 @@ | ||
| # Jewels and Stones | ||
|
|
||
| You're given strings `jewels` representing the types of stones that are jewels, and `stones` representing the stones you have. | ||
| Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels. | ||
|
|
||
| Letters are case-sensitive, so "a" is considered a different type of stone from "A". | ||
|
|
||
| ## Examples | ||
|
|
||
| Example 1: | ||
|
|
||
| ```text | ||
| Input: jewels = "aA", stones = "aAAbbbb" | ||
| Output: 3 | ||
| ``` | ||
|
|
||
| Example 2: | ||
|
|
||
| ```text | ||
| Input: jewels = "z", stones = "ZZ" | ||
| Output: 0 | ||
| ``` | ||
|
|
||
| ## Constraints | ||
|
|
||
| - 1 <= jewels.length, stones.length <= 50 | ||
| - jewels and stones consist of only English letters. | ||
| - All the characters of jewels are unique. | ||
|
|
||
| ## Topics | ||
|
|
||
| - Hash Table | ||
| - String | ||
|
|
||
| ## Solution | ||
|
|
||
| The core intuition behind solving this problem is to treat it as a membership-counting task: we aren’t transforming | ||
| either string, we’re simply counting how many characters in stones belong to the set of jewel types in jewels, while | ||
| respecting case sensitivity. This maps naturally to a hash-based lookup because it lets us store all jewel types in a | ||
| structure that supports fast membership checks. In other words, we treat jewels as an allowlist of valid types and stones | ||
| as a stream of items to evaluate. As we scan through stones, we increment a counter whenever the current character appears | ||
| in the jewel set. As comparisons are case-sensitive, only exact matches contribute to the final count, which represents | ||
| how many of your stones are jewels. | ||
|
|
||
| Using the intuition above, we implement the algorithm as follows: | ||
|
|
||
| 1. Initialize a new set, jewelSet, from the given jewels. | ||
| 2. Initialize a variable count to 0. | ||
| 3. Iterate through each character ch in the stones: | ||
| - If ch exists in jewelSet: | ||
| - Increment count. | ||
| 4. After successfully iterating through the stones array, return count. | ||
|
|
||
| ### Time complexity | ||
|
|
||
| The time complexity of the solution is O(m+n) because it first builds a set from the m characters in jewels, then scans the | ||
| n characters in stones once to count matches. | ||
|
|
||
| ### Space complexity | ||
|
|
||
| The space complexity of the solution is O(m) because it stores up to m unique jewel characters in a set. |
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,34 @@ | ||
| from collections import Counter | ||
| from typing import Set | ||
|
|
||
|
|
||
| def num_jewels_in_stones_with_set(jewels: str, stones: str) -> int: | ||
| # Store all jewel types for fast membership checking | ||
| jewel_set: Set[str] = set(jewels) | ||
|
|
||
| # Count how many stones are jewels | ||
| count = 0 | ||
|
|
||
| # Check each stone and increment count if it's a jewel | ||
| for ch in stones: | ||
| if ch in jewel_set: | ||
| count += 1 | ||
|
|
||
| # Return the total number of jewels found in stones | ||
| return count | ||
|
|
||
|
|
||
| def num_jewels_in_stones_with_dict(jewels: str, stones: str) -> int: | ||
| # Store all jewel types for fast membership checking | ||
| stone_counts: Counter[str] = Counter(stones) | ||
|
|
||
| # Count how many stones are jewels | ||
| count = 0 | ||
|
|
||
| # Check each stone and increment count if it's a jewel | ||
| for jewel in jewels: | ||
| if jewel in stone_counts: | ||
| count += stone_counts[jewel] | ||
|
|
||
| # Return the total number of jewels found in stones | ||
| return count |
34 changes: 34 additions & 0 deletions
34
algorithms/hash_table/jewels_and_stones/test_jewels_and_stones.py
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,34 @@ | ||
| import unittest | ||
| from parameterized import parameterized | ||
| from algorithms.hash_table.jewels_and_stones import ( | ||
| num_jewels_in_stones_with_dict, | ||
| num_jewels_in_stones_with_set, | ||
| ) | ||
|
|
||
| JEWELS_AND_STONES_TEST_CASES = [ | ||
| ("pQ", "ppPQQq", 4), | ||
| ("k", "kkkkK", 4), | ||
| ("LMn", "lLmMNn", 3), | ||
| ("cD", "ddddccccDD", 6), | ||
| ("tRz", "RttZzr", 4), | ||
| ] | ||
|
|
||
|
|
||
| class JewelsAndStonesTestCase(unittest.TestCase): | ||
| @parameterized.expand(JEWELS_AND_STONES_TEST_CASES) | ||
| def test_num_jewels_in_stones_with_set( | ||
| self, jewels: str, stones: str, expected: int | ||
| ): | ||
| actual = num_jewels_in_stones_with_set(jewels, stones) | ||
| self.assertEqual(actual, expected) | ||
|
|
||
| @parameterized.expand(JEWELS_AND_STONES_TEST_CASES) | ||
| def test_num_jewels_in_stones_with_dict( | ||
| self, jewels: str, stones: str, expected: int | ||
| ): | ||
| actual = num_jewels_in_stones_with_dict(jewels, stones) | ||
| self.assertEqual(actual, expected) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.