771. Jewels and Stones

题目:
You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A"

  1. python
def numJewelsInStones(self, J, S):
        setJ = set(J)
        return sum(s in setJ for s in S)
  1. C++
int numJewelsInStones(string J, string S) {
        int res = 0;
        unordered_set<char> setJ(J.begin(),J.end());
        for ( char s:S) if (setJ.count(s)) res++;
        return res;

}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,694评论 0 10
  • sophiakk阅读 313评论 0 0
  • 我困顿于前方,堕落于不甘的屈服。 渴望的乌鸦,看到的是绝望瓶底和那缥缈的石子。 远处的神砥,梦中的她,我一路小跑,...
    许以曹阅读 233评论 0 1
  • 通常秋天是一个丰富多彩的季节,秋天除了是色彩斑斓的,五颜六色的外,还是收获喜悦的季节,分享的季节,让人留念的季节,...
    后半生为自己阅读 219评论 0 1

友情链接更多精彩内容