class Solution:
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
res = 0
for s in S:
if s in J:
res += 1
return res
一行解法:
def numJewelsInStones(self, J, S):
return sum(map(J.count, S))
def numJewelsInStones(self, J, S):
return sum(s in J for s in S)
最优解法:
- 相差的长度即为被替换掉的珠宝种类
class Solution:
def numJewelsInStones(self, J, S):
b=S
for a in J:
b=b.replace(a,'')
return (len(S)-len(b))