Algorithm


Problem Name: 771. Jewels and Stones

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

Letters are case sensitive, so "a" is considered a different type of stone from "A".

 

Example 1:

Input: jewels = "aA", stones = "aAAbbbb"
Output: 3

Example 2:

Input: jewels = "z", stones = "ZZ"
Output: 0

 

Constraints:

  • 1 <= jewels.length, stones.length <= 50
  • jewels and stones consist of only English letters.
  • All the characters of jewels are unique.

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int numJewelsInStones(string J, string S) {
        int count = 0;
        unordered_set < char>s;
        for(char c: J) s.insert(c);
        for(char c: S) if(s.count(c)) count++;
        return count;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
jewels = "aA", stones = "aAAbbbb"

Output

x
+
cmd
3

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int numJewelsInStones(String J, String S) {
    Set set = new HashSet<>();
    for (char c : J.toCharArray()) {
      set.add(c);
    }
    int count = 0;
    for (char c : S.toCharArray()) {
      if (set.contains(c)) {
        count++;
      }
    }
    return count;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
jewels = "aA", stones = "aAAbbbb"

Output

x
+
cmd
3

#3 Code Example with Javascript Programming

Code - Javascript Programming


const numJewelsInStones = function(J, S) {
  if(J == null || J === '' || S == null || S === '') return 0
  const m = new Set(J)
  let res = 0
  for(let e of S) {
    if(m.has(e)) res++
  }
  return res
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
jewels = "z", stones = "ZZ"

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def numJewelsInStones(self, J, S):
        sj = set(J)
        return sum(s in sj for s in S)
Copy The Code & Try With Live Editor

Input

x
+
cmd
jewels = "z", stones = "ZZ"

#5 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _0771_JewelsAndStones
    {
        public int NumJewelsInStones(string J, string S)
        {
            var map = new Dictionary < char, int>();
            foreach (var ch in J)
                map.Add(ch, 0);

            int result = 0;
            foreach (var ch in S)
                if (map.ContainsKey(ch)) result++;

            return result;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
jewels = "aA", stones = "aAAbbbb"

Output

x
+
cmd
3
Advertisements

Demonstration


Previous
#770 Leetcode Basic Calculator IV Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#773 Leetcode Sliding Puzzle Solution in C, C++, Java, JavaScript, Python, C# Leetcode