LeetCode-771. Jewels and Stones

网友投稿 268 2022-08-29

LeetCode-771. Jewels and Stones

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"​​.

Example 1:

Input: J = "aA", S = "aAAbbbb"Output: 3

Example 2:

Input: J = "z", S = "ZZ"Output: 0

Note:

​​S​​​ and​​J​​ will consist of letters and have length at most 50.The characters in​​J​​ are distinct.

题解:

class Solution {public: int numJewelsInStones(string J, string S) { if (J.length() == 0 || S.length() == 0) { return 0; } int res = 0; unordered_set dic; for (int i = 0; i < J.length(); i++) { dic.insert(J[i]); } for (int i = 0; i < S.length(); i++) { if (dic.find(S[i]) != dic.end()) { res++; } } return res; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-112. Path Sum
下一篇:线下门可罗雀,打着华为旗号搞营销,赛力斯AITO能做起来么?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~