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

time: O(M+N), space: O(M)  -- M: # of length of J

class Solution {
    public int numJewelsInStones(String J, String S) {
        Set<Character> set = new HashSet<>();
        for(char c : J.toCharArray()) {
            set.add(c);
        }
        int cnt = 0;
        for(char c : S.toCharArray()) {
            if(set.contains(c)) {
                cnt++;
            }
        }
        return cnt;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10209714.html

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

771. Jewels and Stones - Easy的相关文章

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

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&amp;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

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

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 

[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

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/