Skip to content

Instantly share code, notes, and snippets.

@ahmet-cetinkaya
Last active December 30, 2020 18:04
Show Gist options
  • Save ahmet-cetinkaya/a754c926d3ebf47f48c2b9cfc2cbe5eb to your computer and use it in GitHub Desktop.
Save ahmet-cetinkaya/a754c926d3ebf47f48c2b9cfc2cbe5eb to your computer and use it in GitHub Desktop.
LeetCode Solutions - 771. Jewels and Stones
const numJewelsInStones = (J, S) => {
let c = 0;
for (let i = 0; i < S.length; ++i) if (J.indexOf(S[i]) > -1) ++c;
return c;
};
const numJewelsInStones = (J, S) => S.split``.reduce((a, c) => a + J.includes(c), 0);
const numJewelsInStones = (J, S) => {
const JSet = new Set(J);
return S.split``.reduce((a, c) => a + JSet.has(c), 0);
};
const numJewelsInStones = (J, S) => (S.match(new RegExp(`[${J}]`, 'g')) || []).length;
@ahmet-cetinkaya
Copy link
Author

jewelsAndStones.js

Result
Runtime: 80 ms, faster than 82.72% of JavaScript online submissions for Jewels and Stones.
Memory Usage: 38.4 MB, less than 97.62% of JavaScript online submissions for Jewels and Stones.

jewelsAndStones2.js

Result
Runtime: 76 ms, faster than 93.44% of JavaScript online submissions for Jewels and Stones.
Memory Usage: 38.9 MB, less than 71.81% of JavaScript online submissions for Jewels and Stones.

jewelsAndStones3.js

Result
Runtime: 80 ms, faster than 82.72% of JavaScript online submissions for Jewels and Stones.
Memory Usage: 40.4 MB, less than 32.48% of JavaScript online submissions for Jewels and Stones.

jewelsAndStones4.js

Result
Runtime: 100 ms, faster than 9.42% of JavaScript online submissions for Jewels and Stones.
Memory Usage: 39.3 MB, less than 48.63% of JavaScript online submissions for Jewels and Stones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment