[简单]1.宝石和石头

问题描述

有两个字符串J和S:J代表所有宝石;S代表所有石头。想知道石头里面有多少是宝石?说明:字符串的每个字符代表一个宝石或者石头,字符大小写敏感(即区分大小写),比如:"a"和"A"表示不同的宝石或石头;
示例 1:

Input:J = "aA", S = "aAAbbb"
Output:3

示例 2:

Input: J = "z", S = "ZZ"
Output: 0

问题分析:Python字符串的基本操作,比如统计字符串中特定字符的数量。

详细代码如下:

class Solution(object):

    def numJewelsInStones(self, J, S):
        """
        :param J: type string
        :param S: type string
        :return: type int
        """
        if not (isinstance(J, str) or isinstance(S, str)):
            return -1
        if len(J) > 50 or len(S) > 50:
            return -1
        counter = 0
        for jewel in J:
            counter += S.count(jewel)
        return counter
原文地址:https://www.cnblogs.com/love9527/p/8615953.html