10.Jewels and Stones
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.
Example 1:
Input: J = "aA", S = "aAAbbbb"
Output: 3
Example 2:
Input: J = "z", S = "ZZ"
Output: 0
Solution : (Using Hashset)
class Solution
{
public:
int numJewelsInStones(string J, string S)
{
unordered_set<char> s;
int c = 0;
for (int i = 0; i < J.length(); i++)
{
if (s.find(J[i]) == s.end())
{
s.insert(J[i]);
}
}
for (int i = 0; i < S.length(); i++)
{
if (s.find(S[i]) != s.end())
{
c++;
}
}
return c;
}
};
Time Complexity: O(n), Space Complexity: O(n)
Last updated
Was this helpful?