Skip to content

Commit 72511c5

Browse files
committed
Create 771-jewels_and_stones.py
1 parent 90ec761 commit 72511c5

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

771-jewels_and_stones.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
https://leetcode.com/problems/jewels-and-stones/
3+
4+
"""
5+
class Solution(object):
6+
7+
"""
8+
Strat:
9+
Make a set to store occurances for each jewel. Then, iterate through each stone
10+
to see if it is a jewel.
11+
12+
Stats: O(J + S) time, O(J) space -- iterate thru J and S + set of size J to store Jewels
13+
Runtime: 36 ms, faster than 6.54% of Python online submissions for Jewels and Stones.
14+
Memory Usage: 12.7 MB, less than 76.19% of Python online submissions for Jewels and Stones.
15+
"""
16+
def numJewelsInStones(self, J, S):
17+
"""
18+
:type J: str
19+
:type S: str
20+
:rtype: int
21+
"""
22+
jewels = set()
23+
for jewel in J:
24+
jewels.add(jewel)
25+
26+
num_jewels = 0
27+
for stone in S:
28+
if stone in jewels:
29+
num_jewels += 1
30+
31+
return num_jewels
32+
33+
"""
34+
(it is apparently faster if you actually do a brute force and not make a dictionary)
35+
36+
Stats: O(J * S) time, O(1) space
37+
Runtime: 20 ms, faster than 69.03% of Python online submissions for Jewels and Stones.
38+
Memory Usage: 12.8 MB, less than 36.28% of Python online submissions for Jewels and Stones.
39+
"""
40+
def numJewelsInStones(self, J, S):
41+
"""
42+
:type J: str
43+
:type S: str
44+
:rtype: int
45+
"""
46+
return sum([1 for s in S if s in list(J)])

0 commit comments

Comments
 (0)