Skip to main content
deleted 13 characters in body
Source Link
thadeuszlay
  • 4k
  • 27
  • 51

The task

is taken from leetcode

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 1:

Input: J = "aA", S = "aAAbbbb"

Output: 3

Example 2:

Input: J = "z", S = "ZZ"

Output: 0

Note:

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

My functional solution

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
    const set = new Set(J);
    return [...S].reduce((ac, s) => set.has(s) ?+ ac, +0);
};

My imperative solution:

/**
 1* :@param ac{string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, 0S) {
  let num = 0;
  const set = {};
  for (let j = 0; j < J.length; j++) {
    set[J[j]] = 1;
  }
  for (let s = 0; s < S.length; s++) {
    num += set[S[s]] || 0;
  }
  return num;
};

The task

is taken from leetcode

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 1:

Input: J = "aA", S = "aAAbbbb"

Output: 3

Example 2:

Input: J = "z", S = "ZZ"

Output: 0

Note:

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

My functional solution

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
    const set = new Set(J);
    return [...S].reduce((ac, s) => set.has(s) ? ac + 1 : ac, 0);
};

The task

is taken from leetcode

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 1:

Input: J = "aA", S = "aAAbbbb"

Output: 3

Example 2:

Input: J = "z", S = "ZZ"

Output: 0

Note:

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

My functional solution

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
  const set = new Set(J);
  return [...S].reduce((ac, s) => set.has(s) + ac, 0);
};

My imperative solution:

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
  let num = 0;
  const set = {};
  for (let j = 0; j < J.length; j++) {
    set[J[j]] = 1;
  }
  for (let s = 0; s < S.length; s++) {
    num += set[S[s]] || 0;
  }
  return num;
};
Source Link
thadeuszlay
  • 4k
  • 27
  • 51

Jewels and Stones

The task

is taken from leetcode

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 1:

Input: J = "aA", S = "aAAbbbb"

Output: 3

Example 2:

Input: J = "z", S = "ZZ"

Output: 0

Note:

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

My functional solution

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
    const set = new Set(J);
    return [...S].reduce((ac, s) => set.has(s) ? ac + 1 : ac, 0);
};