LeetCode 771

  1. Create a new hash
  2. Initialize a counter equal to 0
  3. Set the hash with elements in the Jewels string.
  4. Iterate over the Stones string and determine if current iteration is in the Hash. If it is, increment a counter
  5. Return the counter
var numJewelsInStones = function(jewels, stones) {
//step 1
let hash = new Map()
//step 2
let count = 0
//step 3
for (let ele of jewels) {
hash.set(ele, ele)
}
//step 4
for (let i=0; i < stones.length; i++) {
if (hash.get(stones[i])) {
count++
}
}
//step 5
return count
};
let count = 0
for (let ele of jewels) {
hash.set(ele, ele)
}

for (let i=0; i < stones.length; i++) {
if (hash.get(stones[i])) {
count++
}
}
return count
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store