问题描述: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”.
Examples:
Input: J = "aA", S = "aAAbbbb"
Output: 3
思路:利用对象的属性来判断,把J中的每一个字符定义为某个对象的属性,然后遍历S判断S中的每一个字符是在这个对象的属性中
/**
* @param {string} J
* @param {string} S
* @return {number}
*/
var numJewelsInStones = function(J, S) {
var ret = {};
for(j in J){
ret[J[j]] = 1;
}
var num = 0;
for(i in S){
if(S[i] in ret){
num++;
}
}
return num;
};