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. gitlab修改ip

    gitlab 修改ip的两种方式: 修改/etc/gitlab/gitlab.rd 里面的#external_url 'http://gitlab.example.com' 为ip地址,然后重新构建- ...

  2. gulp-htmlmin 页面压缩插件 gulp插件 参数说明

    gulpfile.js var gulp = require('gulp'), htmlmin = require('gulp-htmlmin'); gulp.task('testHtmlmin', ...

  3. “Another git process seems to be running in this repository...”Git此问题解决

    Git中显示:Another git process seems to be running in this repository, e.g.an editor opened by 'git comm ...

  4. Windows环境下设置Tomcat8以服务的形式运行,不再打开Tomcat窗口

    内容简介 在Windows操作系统下,设置Tomcat8以服务的形式运行,按照以下3步来操作即可.前提条件:已安装好Java环境,并配置好java的环境变量:已下载好Tomcat8并解压到某目录. s ...

  5. WinDbg命令窗口的使用

    调试器命令窗口是windbg中的主要调试信息窗口.可以在此窗口中输入调试程序命令并查看命令输出.Windbg的命令窗口是我们进行调试时,主要打交道的窗口.界面如下 对于windbg,“调试器命令窗口” ...

  6. 洛谷 P2563 [AHOI2001]质数和分解 题解

    P2563 [AHOI2001]质数和分解 题目描述 任何大于 1 的自然数 n 都可以写成若干个大于等于 2 且小于等于 n 的质数之和表达式(包括只有一个数构成的和表达式的情况),并且可能有不止一 ...

  7. javascript 中的方法注入

    js 中的方法注入 java中很多框架支持 apo 的注入, js中也可以类似的进行实现 主要是通过扩展js中方法的老祖 Function 对象来进行实现. Function.prototype.af ...

  8. nRF51822 Beacon 扫描请求包的设置

    Nordic 公司自己做有 iBeacon的板子和 SDK,很少有人拥有这个SDK,我最近在朋友那也拿到了一个,但是还没有时间看. 现在我们用普通的SDK自带的 Beacon 例程来做开发,开发的时相 ...

  9. React中父子组件传值

    一.首先我们先来看父组件向子组件传值 1.1 我们要明白父组件 --> 子组件 是通过props这个属性来传值的 我们来看父组件的代码 import React from 'react'; im ...

  10. SDN第六次上机作业

    1.实验拓扑 实验拓扑图如下: 搭建代码如下: 创建py脚本文件,并编写代码,如下: class MyTopo(Topo): def __init__(self): # initilaize topo ...