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".
classSolution {
publicintnumJewelsInStones(String jewels, String stones) {
Set<Character> s =new HashSet<>();
for (char c : jewels.toCharArray()) s.add(c);
int ans = 0;
for (char c : stones.toCharArray()) if (s.contains(c)) ans++;
return ans;
}
}
1
2
3
4
5
6
classSolution {
funnumJewelsInStones(jewels: String, stones: String): Int {
val s = jewels.toSet()
return stones.count { itin s }
}
}
1
2
3
defnum_jewels_in_stones(jewels: str, stones: str) -> int:
s = set(jewels)
return sum(c in s for c in stones)
1
2
3
4
5
use std::collections::HashSet;
fnnum_jewels_in_stones(jewels: &str, stones: &str) -> i32 {
let s: HashSet<char>= jewels.chars().collect();
stones.chars().filter(|c| s.contains(c)).count() asi32}
1
2
3
4
5
6
7
8
classSolution {
numJewelsInStones(jewels: string, stones: string):number {
consts=newSet(jewels);
letans=0;
for (constcofstones) if (s.has(c)) ans++;
returnans;
}
}