ZhuangER
9/12/2018 - 8:57 PM

771. Jewels and Stones

[771. Jewels and Stones] #tags: leetcode, leetcode/Hash Table

// basic solution
// time complexity: sort O(nlogn), map + filter O(n^2)
// total time complexity: O(n^2)


/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
  const sortedS = _.sortBy(S);

  return _.map(J, j => {
    return _.filter(sortedS, s => s === j).length;
  })
  .reduce((len1, len2) => len1 + len2)
};