1. 程式人生 > >1. Jewels and Stones

1. Jewels and Stones

在這裡插入圖片描述

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)