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
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0700-0799/0771-jewels-and-stones.md
+10-18Lines changed: 10 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,15 @@
1
1
---
2
2
id: jewels-and-stones
3
-
title: Jewels and Stones
3
+
title: Jewels and Stones
4
4
sidebar_label: 771 Jewels and Stones
5
-
6
5
tags:
7
6
- HashTable
8
7
- String
9
-
10
8
description: "This is a solution to the Jewels and Stones on leetcode"
11
9
---
12
10
13
11
## Problem Description
12
+
14
13
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
14
Letters are case sensitive, so "a" is considered a different type of stone from "A".
16
15
@@ -43,18 +42,17 @@ Output: 0
43
42
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
43
45
44
### 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.
45
+
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
46
48
47
49
48
#### Complexity Analysis
50
49
51
50
- Time Complexity: $O(J+S)$, where J is the length of jewels and S is the length of stones.
52
51
- Space Complexity: $O(J)$ , where J is no of distinct characters in jewels.
53
52
54
-
## Code in Different Languages
55
-
56
-
<Tabs>
53
+
## Code in Different Languages
57
54
55
+
<Tabs>
58
56
<TabItemvalue="Java"label="Java">
59
57
<SolutionAuthorname="@ImmidiSivani" />
60
58
@@ -74,13 +72,11 @@ We use a hash-based data structure (an unordered_set in C++ or a HashSet in Java
74
72
}
75
73
}
76
74
return count;
77
-
}}
78
-
75
+
}}
79
76
80
77
```
81
78
82
79
</TabItem>
83
-
84
80
<TabItem value="Python" label="Python">
85
81
<SolutionAuthor name="@ImmidiSivani"/>
86
82
@@ -96,13 +92,11 @@ We use a hash-based data structure (an unordered_set in C++ or a HashSet in Java
96
92
return c
97
93
98
94
```
99
-
100
95
</TabItem>
101
-
102
-
<TabItem value="c++" label="c++">
96
+
<TabItem value="cpp" label="c++">
103
97
<SolutionAuthor name="@ImmidiSivani" />
104
98
105
-
```c++
99
+
```cpp
106
100
#include <iostream>
107
101
#include <unordered_set>
108
102
@@ -121,12 +115,10 @@ We use a hash-based data structure (an unordered_set in C++ or a HashSet in Java
121
115
};
122
116
123
117
```
124
-
125
118
</TabItem>
126
-
127
-
</Tabs>
119
+
</Tabs>
128
120
129
121
## References
130
122
131
123
-**LeetCodeProblem**: [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/solutions/)
0 commit comments