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".

Example 1:

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

Example 2:

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

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

给两个字符串,珠宝字符串J和石头字符串S,其中J中的每个字符都是珠宝,S中的每个字符都是石头,区分字母的大小写,问我们S中有多少个珠宝。

Java:

public int numJewelsInStones(String J, String S) {
return S.replaceAll("[^" + J + "]", "").length();
}

Java:

public int numJewelsInStones(String J, String S) {
int res = 0;
Set setJ = new HashSet();
for (char j: J.toCharArray()) setJ.add(j);
for (char s: S.toCharArray()) if (setJ.contains(s)) res++;
return res;
} 

Python:

def numJewelsInStones(self, J, S):
return sum(map(J.count, S))

Python:

def numJewelsInStones(self, J, S):
return sum(map(S.count, J))

Python:  

def numJewelsInStones(self, J, S):
return sum(s in J for s in S) 

Python:

def numJewelsInStones(self, J, S):
setJ = set(J)
return sum(s in setJ for s in S)  

Python:

# Time:  O(m + n)
# Space: O(n)
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)  

Python: wo

class Solution(object):
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
res = 0
for i in S:
if i in J:
res += 1 return res

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;
}

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 771. Jewels and Stones 珠宝和石头的更多相关文章

  1. LeetCode 771. Jewels and Stones (宝石与石头)

    题目标签:Hash Table 这一题很简单,题目给了两个string:J 和 S. 只要把J 里面的 char 放入HashSet,再遍历S找出有多少个石头是宝石. Java Solution: R ...

  2. Leetcode#771.Jewels and Stones(宝石与石头)

    题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...

  3. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

  4. [LeetCode] Jewels and Stones 珠宝和石头

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  5. 771. Jewels and Stones珠宝数组和石头数组中的字母对应

    [抄题]: You're given strings J representing the types of stones that are jewels, and S representing th ...

  6. 【LeetCode】Jewels and Stones(宝石与石头)

    这道题是LeetCode里的第771道题. 题目要求: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝 ...

  7. LeetCode 771 Jewels and Stones 解题报告

    题目要求 You're given strings J representing the types of stones that are jewels, and S representing the ...

  8. 771. Jewels and Stones - LeetCode

    Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...

  9. 【Leetcode_easy】771. Jewels and Stones

    problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...

随机推荐

  1. SQL异常总结

    1.resultType和resultMap写错时,启动时就会报错 原因: 2.The error occurred while handling results ### SQL: select US ...

  2. linux添加用户adduser出现 useradd:cannot lock /etc/passwd; try again

    找一下有个叫/etc/passwd.lock的文件,找到,给它用root删掉就好了,可能是上次使用到这个文件没有正常关闭.具体操作:切换到root用户,用cd etc到etc目录下,rm .pwd.l ...

  3. LeetCode 959. Regions Cut By Slashes

    原题链接在这里:https://leetcode.com/problems/regions-cut-by-slashes/ 题目: In a N x N grid composed of 1 x 1 ...

  4. 用 ThreadPoolExecutor/ThreadPoolTaskExecutor 线程池技术提高系统吞吐量(附带线程池参数详解和使用注意事项)

    1.概述 在Java中,我们一般通过集成Thread类和实现Runnnable接口,调用线程的start()方法实现线程的启动.但如果并发的数量很多,而且每个线程都是执行很短的时间便结束了,那样频繁的 ...

  5. 个人Vim配置(即vim目录下vimrc_)

    因为是C++选手所以大部分带有Dev遗留的...格式 colorscheme molokai"配色方案,注意molokai不是自带而是自己调配的,SublimeText3标准配色,想要的点这 ...

  6. 洛谷 CF894A QAQ

    目录 题目 思路 \(Code\) 题目 CF894A 思路 \(\text{DP}\) 一个数组\(\text{QAQ[4][101]}\) \(\text{QAQ[1][i]表示在i这个位置q的个 ...

  7. 【JZOJ6229】【20190621】san

    题目 \(n\)个点\(m\)条边的有向图,每个点有点权 你可以选择拓扑序的一个区间的 最大化点权和 $n \le 50  , m \le \frac{n*(n-1)}{2} , 0 \le |a_i ...

  8. dropbox icloud and nustore

    dropbox icloud and nustore 这里只是写一下自己的感受. 曾经搜索无数遍, 想着用哪个比较好, 想来比较一下, 还不如自己直接用用看吧. 于是同时用了很久的 dropbx 和 ...

  9. GoCN每日新闻(2019-10-19)

    GoCN每日新闻(2019-10-19) Go 1.13中的错误处理 https://tonybai.com/2019/10/18/errors-handling-in-go-1-13 golang核 ...

  10. Dense Semantic Labeling with Atrous Spatial Pyramid Pooling and Decoder for High-Resolution Remote Sensing Imagery(高分辨率语义分割)

    对 Potsdam and Vaihingen 公开数据集进行处理,得到了SOTA的结果,超越DeepLab_v3+,提出的网络结构如下:结合了ASPP和FCN,UNet