Skip to content

Commit 8d168bf

Browse files
committed
ID: 771 Jewels And Stones (Hash Table)
1 parent 6daedf0 commit 8d168bf

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Hash_Table/0771_Jewels_And_Stones.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
You're given strings J representing the types of stones that are jewels, and S representing the
3+
stones you have. Each character in S is a type of stone you have. You want to know how many of
4+
the stones you have are also jewels.
5+
The letters in J are guaranteed distinct, and all characters in J and S are letters.
6+
Letters are case sensitive, so "a" is considered a different type of stone from "A".
7+
8+
Input: J = "aA", S = "aAAbbbb"
9+
Output: 3
10+
11+
Input: J = "z", S = "ZZ"
12+
Output: 0
13+
'''
14+
15+
class Solution:
16+
''' Approach #1 (Hash Table) '''
17+
'''
18+
Store every Jewel as a key in the Hash Table
19+
Iterate over stones and in the jewel count if stone is present in the Hash
20+
'''
21+
def numJewelsInStones(self, J: str, S: str) -> int:
22+
Hash = dict()
23+
jewelCount = 0
24+
for jewel in J:
25+
Hash[jewel] = True
26+
27+
for stone in S:
28+
jewelCount += 1 if(stone in Hash) else 0
29+
30+
return jewelCount
31+
32+
if __name__ == '__main__':
33+
sol = Solution()
34+
J = "aA"; S = "aAAbbbb"
35+
print(sol.numJewelsInStones(J,S))

0 commit comments

Comments
 (0)