Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 688 Bytes

Jewels and Stones.md

File metadata and controls

33 lines (26 loc) · 688 Bytes

Algorithm

  1. Put jewels in a Set for O(1) access.
  2. For each stone, check if it is a jewel.

Solution

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/Space Complexity

  • Time Complexity: O(j + s)
  • Space Complexity: O(j)

Links