题目描述
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”.
给定用例
Input: J = "aA", S = "aAAbbbb"
Output: 3
Input: J = "z", S = "ZZ"
Output: 0
题目说明
给定一个字符串,里面的每一个字母代表一种宝物,玩家手中也有一个字符串,这个字符串中的每一个字母代表一个石头,看看玩家手中的石头中有多少宝物
我的代码
/**
* @param {string} J
* @param {string} S
* @return {number}
*/
var numJewelsInStones = function(J, S) {
var sourceArray = S.split('');
var retNumber = 0;
for(var index = 0; index < sourceArray.length; index++)
{
if(J.indexOf(sourceArray[index]) != -1) retNumber++;
}
return retNumber;
};