Skip to content

Commit 56a980a

Browse files
Create Day 2 Jewels and Stones.cpp
1 parent 428f340 commit 56a980a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Day 2 Jewels and Stones.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
PROBLEM:
2+
3+
You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in
4+
S is a type of stone you have. You want to know how many of the stones you have are also jewels.
5+
6+
The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered
7+
a different type of stone from "A".
8+
9+
Example 1:
10+
11+
Input: J = "aA", S = "aAAbbbb"
12+
Output: 3
13+
Example 2:
14+
15+
Input: J = "z", S = "ZZ"
16+
Output: 0
17+
18+
19+
SOLUTION:
20+
21+
22+
class Solution {
23+
public:
24+
int numJewelsInStones(string J, string S) {
25+
26+
unordered_set<char> s;
27+
int i,n,m,ans=0;
28+
29+
m=J.length();
30+
n=S.length();
31+
32+
for(i=0;i<m;i++)
33+
s.insert(J[i]);
34+
35+
36+
for(i=0;i<n;i++)
37+
{
38+
if(s.find(S[i])!=s.end())
39+
{
40+
ans++;
41+
}
42+
}
43+
44+
return ans;
45+
}
46+
};

0 commit comments

Comments
 (0)