Jewels and Stones
Description
code:
class Solution {
public int numJewelsInStones(String J, String S) {
Map<Character, Integer> map = new HashMap<>();
char[] s = S.toCharArray();
for (int i = 0; i < s.length; i++) {
map.put(s[i], map.getOrDefault(s[i], 0) + 1);
}
int count = 0;
char[] j = J.toCharArray();
for (int i = 0; i < j.length; i++) {
if (map.containsKey(j[i])) {
count += map.get(j[i]);
}
}
return count;
}
}Last updated