[LeetCode] 771. Jewels and Stones 珠宝和石头
LeetCode and 石头
2023-09-27 14:28:37 时间
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
andJ
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: Find And Replace in String
- Leetcode: Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode: Best Time to Buy and Sell Stock II
- JS Leetcode 198. 打家劫舍 题解分析,再次感受动态规划的魅力
- [LeetCode] Maximum Depth of Binary Tree
- [LeetCode]5384. 拥有最多糖果的孩子
- LeetCode Summary Ranges
- Leetcode Best Time to Buy and Sell Stock II
- [LeetCode] 856. Score of Parentheses 括号的分数
- [LeetCode] 841. Keys and Rooms 钥匙与房间
- [LeetCode] Jewels and Stones 珠宝和石头
- [LeetCode] Delete and Earn 删除与赚取
- [LeetCode] 1-bit and 2-bit Characters 一位和两位字符
- [LeetCode] 503. Next Greater Element II 下一个较大的元素之二
- [LeetCode] Minimum Height Trees 最小高度树
- [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
- [LeetCode] 289. Game of Life 生命游戏
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
- leetcode 380. Insert Delete GetRandom O(1) O(1) 时间插入、删除和获取随机元素 (中等)
- leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树(中等)
- leetcode 34. Find First and Last Position of Element in Sorted Array 在排序数组中查找元素的第一个和最后一个位置(中等)