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".
Example 1:
Input: jewels = "aA", stones = "aAAbbbb" Output: 3
Example 2:
Input: jewels = "z", stones = "ZZ" Output: 0
Constraints:
1 <= jewels.length, stones.length <= 50jewels and stones consist of only English letters.jewels are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to count how many stones are also jewels. The simplest way to do this is to look at each stone one by one and check if it's a jewel.
Here's how the algorithm would work step-by-step:
def jewels_and_stones_brute_force(jewels, stones):
jewel_count = 0
# Iterate through each stone to check if it is a jewel
for each_stone in stones:
# Check if the current stone is in the jewels string
if each_stone in jewels:
# Increment the jewel count if the stone is a jewel
jewel_count += 1
return jewel_countThe most efficient way to solve this problem is to first identify each unique type of 'jewel' and then count how many of those jewels are present in the 'stones' you have. We avoid redundant comparisons by creating a quick way to check if a stone is a jewel.
Here's how the algorithm would work step-by-step:
def jewels_and_stones(jewels, stones): jewels_set = set(jewels)
jewel_count = 0
# Iterate through each stone to check if it's a jewel.
for stone in stones:
# Check if the current stone is present in the set of jewels.
if stone in jewels_set:
jewel_count += 1
return jewel_count| Case | How to Handle |
|---|---|
| jewels is null or empty | Return 0 if `jewels` is null or empty because no stones can be jewels. |
| stones is null or empty | Return 0 if `stones` is null or empty because there are no stones to check. |
| Both jewels and stones are empty strings | Return 0 as there are no jewels or stones. |
| jewels and stones contain very long strings (e.g., length exceeding maximum string length) | Ensure the chosen data structure (e.g., HashSet) has sufficient capacity and time complexity remains acceptable (O(n+m) using hashset). |
| jewels contains duplicate characters | The count will be correct since we are iterating over stones and checking if each stone is present in jewels, handling duplicate jewels without problems. |
| stones contains duplicate characters | Each duplicate stone will be counted if it is also a jewel; this is expected behavior. |
| jewels and stones contain Unicode characters | The solution should work correctly with Unicode characters assuming the language and data structures (String, Set) support them. |
| All stones are jewels (stones contains only characters present in jewels) | The solution should correctly count all stones as jewels, returning stones.length(). |