euisblue
771. Jewels and Stones

C++

1class Solution { 2public: 3 int numJewelsInStones(string J, string S) { 4 int cnt = 0; 5 for(int i=0; S[i]; ++i) 6 { 7 for(int j=0; j<J.size(); ++j) 8 { 9 if(S[i] == J[j]) ++cnt; 10 } 11 } 12 13 return cnt; 14 } 15};

JavaScript

1var numJewelsInStones = function(J, S) { 2 let cnt = 0; 3 S = S.split(''); 4 S.forEach(c => { 5 (J.includes(c)) ? cnt += 1 : cnt; 6 }) 7 8 return cnt; 9};

Ruby

1def num_jewels_in_stones(j, s) 2 cnt = 0 3 s = s.split('') 4 for c in s do 5 if j.include?(c) then cnt += 1 end 6 end 7 cnt 8end