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

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.
  1. class Solution:
  2. def numJewelsInStones(self, J, S):
  3. """
  4. :type J: str
  5. :type S: str
  6. :rtype: int
  7. """
  8. res = 0
  9. s = set([(i) for i in J])
  10. for i in S:
  11. if i in s:
  12. res+=1
  13. return res

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8379840.html

时间: 2024-08-30 15:50:36

771. Jewels and Stones 珠宝和石头的相关文章

Leetcode #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 Sis 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

[LeetCode&Python] Problem 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 Sis 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

771. Jewels and Stones - Easy

You're given strings J representing the types of stones that are jewels, and Srepresenting 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

letCode(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

【博客搬家旧文】leetcode 771. Jewels and Stones

今天开通了博客园 ,之前的博客就不用了.之后再陆陆续续把之前的博文重新放到这里来.有标题这个tag的都是搬运的旧博客的文章.希望在这里是个新的开始,嘻嘻. import java.util.Scanner; class Solution { public static int numJewelsInStones(String J, String S) { int count=0; for(int i=0; i<J.length(); i++){ for(int j=0; j<S.length(

codechef Jewels and Stones 题解

Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood - now she has become really good with identifying which ones are fake and which ones are no

[Swift]LeetCode771. 宝石与石头 | 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

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 Sis a type of stone you have.  You want to know how many of the stones you have are also jewels. The letters in 

leetCode题解之Jewels and Stones

1.题目描述 2.分析 使用HashTable 是解决这种的好方法. 3.代码 1 int numJewelsInStones(string J, string S) { 2 map<char,int> m; 3 for(char c : S) 4 m[c]++; 5 6 int ans = 0; 7 for( char c: J) 8 ans += m[c]; 9 return ans; 10 11 } 原文地址:https://www.cnblogs.com/wangxiaoyong/p/