Jewels and Stones
Input: J = "aA", S = "aAAbbbb"
Output: 3Input: J = "z", S = "ZZ"
Output: 0class Solution {
public int numJewelsInStones(String J, String S) {
// create set of jewels and then iterate through S, and check if a letter is jewel, if yes, increment counter
Set<Character> jewels = J.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.toSet());
int count = 0;
for (int i = 0; i < S.length(); i++) {
if (jewels.contains(S.charAt(i))) {
count++;
}
}
return count;
}
}Last updated