- Put jewels in a
Set
for O(1) access. - For each stone, check if it is a jewel.
class Solution {
public int numJewelsInStones(String j, String s) {
Set<Character> jewels = new HashSet();
for (int i = 0; i < j.length(); i++) {
jewels.add(j.charAt(i));
}
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (jewels.contains(s.charAt(i))) {
count++;
}
}
return count;
}
}
- Time Complexity: O(j + s)
- Space Complexity: O(j)