DEV Community

Aroup Goldar Dhruba
Aroup Goldar Dhruba

Posted on • Edited on

LeetCode: Jewels and Stones

Problem Statement

You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

Example

Example 1:

Input: J = "aA", S = "aAAbbbb" Output: 3 
Enter fullscreen mode Exit fullscreen mode

Example 2:

Input: J = "z", S = "ZZ" Output: 0 
Enter fullscreen mode Exit fullscreen mode

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

Solution Thought Process

This is a well-known hash problem. First, we take the jewel string and record the frequencies in the hash set. Then we go through the S string to find out if this is a jewel by checking the set one by one, increasing the count of the result by one.

We can find the items in the unordered_set in O(1) time.

Solution

class Solution { public: int numJewelsInStones(string J, string S) { unordered_set<char>jewels; int result = 0; for(int i=0;i<J.size();i++) { jewels.insert(J[i]); } for(int i=0;i<S.size();i++) { if(jewels.find(S[i])!=jewels.end()) { result++; } } return result; } }; 
Enter fullscreen mode Exit fullscreen mode

Complexity

Time Complexity: O(m+n) where m = length of J, n = length of S

Space Complexity: O(m) where m = length of J

Top comments (0)