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.
这题的思路就是同时遍历字符串S和J,如果遍历S的时候发现该字符也出现在J中,则将计数器加1,最后返回计数器的值。
Code:
优化点的解决方案是把J转化成set对象,这样lookup的复杂度就减少为O(1)了,参考代码: