Leetcode: Jewels and Stones

/**
* @param {string} J
* @param {string} S
* @return {number}
*/
var numJewelsInStones = function(J, S) {
let jewels = J.split("");
let stones = S.split("");
let ans = 0;

for(let i = 0; i < jewels.length; i++){
for(let j = 0; j < stones.length; j++){
// if (jewels[i] === stones[j]){
// ans++;
// }
(jewels[i] === stones[j]) ? ans++ : null;
}
}

return ans;

// let ans = 0;

// for(let char of S){
// // J contains char stone
// // Chars is J are distinct from one another
// if (J.lastIndexOf(char) > -1){
// ans++;
// }
// }

// return ans;
};
Input: J = "aA", S = "aAAbbbb"
Output: 3
Input: J = "z", S = "ZZ"
Output: 0

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

from Buffer

Zebra Printer ZD420 Integration & Implementation in iOS (Swift)

Twitter scraping and saving obtained data to Notion with Python

Why Companies are Adopting GraphQL

Top 5 Useful Flutter Libraries

6 Python Projects to Improve Your Skills and Kill Some Time

C#: Why do we need Abstract Class: Purpose of Abstract Class with Real-life Example

Object-Oriented Programming In Java

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
Lawson Hung

Lawson Hung

Software Engineer

More from Medium

The A Star Algorithm

Leetcode Problem 3 — Longest substring without repeating characters

An Introduction to Linked Lists

Backtracking