##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/771.html

# 771. Jewels and Stones

Easy

## Description

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.

## Solution

If either J or S is empty, return 0.

For each letter in S, check whether the letter is in J. If so, the letter represents a jewel, so add the counter by 1. Finally, return the counter.

• public class Jewels_and_Stones {

public class Solution {
/**
* @param J: the types of stones that are jewels
* @param S: representing the stones you have
* @return: how many of the stones you have are also jewels
*/
public int numJewelsInStones(String J, String S) {
if (J == null || J.length() == 0 || S == null || S.length() == 0) { return 0; }
// replace all non-from-J characters from S, then compute its' length
return S.replaceAll("[^" + J + "]", "").length();
}
}

class Solution_brute_force {
public int numJewelsInStones(String J, String S) {
if (J == null || S == null) {
return 0;
}

char[] jArray = J.toCharArray();
char[] sArray = S.toCharArray();

int count = 0;
for (char c: sArray) {
for (char target: jArray) {
if (c == target) {
count++;
break;
}
}
}

return count;
}
}
}

############

class Solution {
public int numJewelsInStones(String jewels, String stones) {
int[] s = new int[128];
for (char c : jewels.toCharArray()) {
s[c] = 1;
}
int ans = 0;
for (char c : stones.toCharArray()) {
ans += s[c];
}
return ans;
}
}

• // OJ: https://leetcode.com/problems/jewels-and-stones/
// Time: O(N)
// Space: O(N)
class Solution {
public:
int numJewelsInStones(string J, string S) {
unordered_set<char> s(J.begin(), J.end());
int ans = 0;
for (char c : S) ans += s.count(c);
return ans;
}
};

• class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
s = set(jewels)
return sum(c in s for c in stones)

############

class Solution(object):
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
return sum(S.count(j) for j in J)

• func numJewelsInStones(jewels string, stones string) (ans int) {
s := make([]int, 128)
for _, c := range jewels {
s[c] = 1
}
for _, c := range stones {
ans += s[c]
}
return
}

• function numJewelsInStones(jewels: string, stones: string): number {
const set = new Set([...jewels]);
let ans = 0;
for (const c of stones) {
set.has(c) && ans++;
}
return ans;
}


• /**
* @param {string} jewels
* @param {string} stones
* @return {number}
*/
var numJewelsInStones = function (jewels, stones) {
const s = new Set(jewels.split(''));
return stones.split('').reduce((prev, val) => prev + s.has(val), 0);
};


• use std::collections::HashSet;
impl Solution {
pub fn num_jewels_in_stones(jewels: String, stones: String) -> i32 {
let mut set = jewels.as_bytes().iter().collect::<HashSet<&u8>>();
let mut ans = 0;
for c in stones.as_bytes() {
if set.contains(c) {
ans += 1;
}
}
ans
}
}


• class Solution {
public int numJewelsInStones(String J, String S) {
if (J == null || S == null || J.length() == 0 || S.length() == 0)
return 0;
int jewels = 0;
int length = S.length();
for (int i = 0; i < length; i++) {
char c = S.charAt(i);
int index = J.indexOf(c);
if (index >= 0)
jewels++;
}
return jewels;
}
}

############

class Solution {
public int numJewelsInStones(String jewels, String stones) {
int[] s = new int[128];
for (char c : jewels.toCharArray()) {
s[c] = 1;
}
int ans = 0;
for (char c : stones.toCharArray()) {
ans += s[c];
}
return ans;
}
}

• // OJ: https://leetcode.com/problems/jewels-and-stones/
// Time: O(N)
// Space: O(N)
class Solution {
public:
int numJewelsInStones(string J, string S) {
unordered_set<char> s(J.begin(), J.end());
int ans = 0;
for (char c : S) ans += s.count(c);
return ans;
}
};

• class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
s = set(jewels)
return sum(c in s for c in stones)

############

class Solution(object):
def numJewelsInStones(self, J, S):
"""
:type J: str
:type S: str
:rtype: int
"""
return sum(S.count(j) for j in J)

• func numJewelsInStones(jewels string, stones string) (ans int) {
s := make([]int, 128)
for _, c := range jewels {
s[c] = 1
}
for _, c := range stones {
ans += s[c]
}
return
}

• function numJewelsInStones(jewels: string, stones: string): number {
const set = new Set([...jewels]);
let ans = 0;
for (const c of stones) {
set.has(c) && ans++;
}
return ans;
}


• /**
* @param {string} jewels
* @param {string} stones
* @return {number}
*/
var numJewelsInStones = function (jewels, stones) {
const s = new Set(jewels.split(''));
return stones.split('').reduce((prev, val) => prev + s.has(val), 0);
};


• use std::collections::HashSet;
impl Solution {
pub fn num_jewels_in_stones(jewels: String, stones: String) -> i32 {
let mut set = jewels.as_bytes().iter().collect::<HashSet<&u8>>();
let mut ans = 0;
for c in stones.as_bytes() {
if set.contains(c) {
ans += 1;
}
}
ans
}
}