加载中...
771-宝石与石头(Jewels and Stones)
发表于:2021-12-03 | 分类: 简单
字数统计: 383 | 阅读时长: 1分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/jewels-and-stones

英文原文

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

Letters are case sensitive, so "a" is considered a different type of stone from "A".

 

Example 1:

Input: jewels = "aA", stones = "aAAbbbb"
Output: 3

Example 2:

Input: jewels = "z", stones = "ZZ"
Output: 0

 

Constraints:

  • 1 <= jewels.length, stones.length <= 50
  • jewels and stones consist of only English letters.
  • All the characters of jewels are unique.

中文题目

 给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。 stones 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

字母区分大小写,因此 "a""A" 是不同类型的石头。

 

示例 1:

输入:jewels = "aA", stones = "aAAbbbb"
输出:3

示例 2:

输入:jewels = "z", stones = "ZZ"
输出:0

 

提示:

  • 1 <= jewels.length, stones.length <= 50
  • jewelsstones 仅由英文字母组成
  • jewels 中的所有字符都是 唯一的

通过代码

高赞题解

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
    let newS=S;
    for(let i=0;i<J.length;i++){
       newS= newS.replace(new RegExp(J[i],'g'),"");
    }
    return S.length-newS.length
};

统计信息

通过次数 提交次数 AC比率
155944 183212 85.1%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
700-二叉搜索树中的搜索(Search in a Binary Search Tree)
下一篇:
701-二叉搜索树中的插入操作(Insert into a Binary Search Tree)
本文目录
本文目录