-
Notifications
You must be signed in to change notification settings - Fork 43
/
jewels-and-stones.py
75 lines (57 loc) · 1.63 KB
/
jewels-and-stones.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
771. Jewels and Stones
Easy
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.
Letters are case sensitive, so "a" is considered a different type of stone from "A".
Example 1:
Input: jewels = "aA", stones = "aAAbbbb"
Output: 3
Example 2:
Input: jewels = "z", stones = "ZZ"
Output: 0
Constraints:
1 <= jewels.length, stones.length <= 50
jewels and stones consist of only English letters.
All the characters of jewels are unique.
"""
# V0
class Solution(object):
def numJewelsInStones(self, J, S):
return len([ i for i in S if i in J])
# V0'
class Solution(object):
def numJewelsInStones(self, J, S):
lookup = set(J)
return sum(s in lookup for s in S)
# V1
# http://bookshadow.com/weblog/2018/01/28/leetcode-jewels-and-stones/
# IDEA : GREEDY
class Solution(object):
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
return sum(s in J for s in S)
# V1'
class Solution(object):
def numJewelsInStones(self, J, S):
output = []
J_list = list(J)
for i in S:
if i in J_list:
output.append(i)
else:
pass
return len(output)
# V2
class Solution(object):
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
lookup = set(J)
return sum(s in lookup for s in S)