Skip to content

Commit 43e43b8

Browse files
committed
Create 0771-Jewels-and-stones
1 parent 3fe121a commit 43e43b8

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
id: jewels-and-stones
3+
title: Jewels and Stones
4+
sidebar_label: 771 Jewels and Stones
5+
6+
tags:
7+
- HashTable
8+
- String
9+
10+
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) {
65+
HashSet<Character> jewelSet = new HashSet<>();
66+
for (char jewel : jewels.toCharArray()) {
67+
jewelSet.add(jewel);
68+
}
69+
70+
int count = 0;
71+
for (char stone : stones.toCharArray()) {
72+
if (jewelSet.contains(stone)) {
73+
count++;
74+
}
75+
}
76+
return count;
77+
}}
78+
79+
80+
```
81+
82+
</TabItem>
83+
84+
<TabItem value="Python" label="Python">
85+
<SolutionAuthor name="@ImmidiSivani" />
86+
87+
```python
88+
class Solution:
89+
def numJewelsInStones(self, jewels: str, stones: str) -> int:
90+
jewels1=list(jewels)
91+
stones1=list(stones)
92+
c=0
93+
for i in stones1:
94+
if i in jewels:
95+
c+=1
96+
return c
97+
98+
```
99+
100+
</TabItem>
101+
102+
<TabItem value="c++" label="c++">
103+
<SolutionAuthor name="@ImmidiSivani" />
104+
105+
```c++
106+
#include <iostream>
107+
#include <unordered_set>
108+
109+
class Solution {
110+
public:
111+
int numJewelsInStones(std::string jewels, std::string stones) {
112+
std::unordered_set<char> jewelSet(jewels.begin(), jewels.end());
113+
int count = 0;
114+
for (char stone : stones) {
115+
if (jewelSet.find(stone) != jewelSet.end()) {
116+
count++;
117+
}
118+
}
119+
return count;
120+
}
121+
};
122+
123+
```
124+
125+
</TabItem>
126+
127+
</Tabs>
128+
129+
## References
130+
131+
- **LeetCode Problem**: [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/solutions/)
132+
- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/jewels-and-stones/post-solution/?submissionId=1279731526)

0 commit comments

Comments
 (0)