You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "This is a solution to the Jewels and Stones on leetcode"
11
+
---
12
+
13
+
## Problem Description
14
+
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.
15
+
Letters are case sensitive, so "a" is considered a different type of stone from "A".
16
+
17
+
18
+
### Examples
19
+
20
+
**Example 1:**
21
+
```
22
+
Input: jewels = "aA", stones = "aAAbbbb"
23
+
Output: 3
24
+
```
25
+
26
+
**Example 2:**
27
+
```
28
+
Input: jewels = "z", stones = "ZZ"
29
+
Output: 0
30
+
31
+
```
32
+
33
+
34
+
35
+
### Constraints
36
+
- `1 <= jewels.length, stones.length <= 50`
37
+
- `jewels and stones consist of only English letters.`
38
+
- `All the characters of jewels are unique.`
39
+
40
+
41
+
## Solution for Koko Eating Bananas
42
+
### Intuition
43
+
The problem requires us to determine how many characters from the jewels string are present in the stones string. Each character in jewels represents a type of jewel, and each character in stones represents a stone that may contain one of these jewels. The goal is to count how many stones are jewels.
44
+
45
+
### Approach
46
+
We use a hash-based data structure (an unordered_set in C++ or a HashSet in Java) to store characters from the jewels string. This allows for average O(1) time complexity for membership checks.and we iterate to each character in stones string if that character is present in jewel we increment the count and we return count.
47
+
48
+
49
+
#### Complexity Analysis
50
+
51
+
- Time Complexity: $O(J+S)$, where J is the length of jewels and S is the length of stones.
52
+
- Space Complexity: $O(J)$ , where J is no of distinct characters in jewels.
53
+
54
+
## Code in Different Languages
55
+
56
+
<Tabs>
57
+
58
+
<TabItem value="Java" label="Java">
59
+
<SolutionAuthor name="@ImmidiSivani" />
60
+
61
+
```java
62
+
import java.util.*;
63
+
class Solution {
64
+
public int numJewelsInStones(String jewels, String stones) {
0 commit comments