###### tags: `Leetcode` `easy` `hash` `python` `c++`
# 771. Jewels and Stones
## [題目連結:] https://leetcode.com/problems/jewels-and-stones/description/
## 題目:
You're given strings ```jewels``` representing the types of ```stones``` that are jewels, and stones representing the stones you have. Each character in ```stones``` is a type of stone you have. You want to know how many of the stones you have are also jewels.
Letters are case sensitive, so ```"a"``` is considered a different type of stone from ```"A"```.
**Example 1:**
```
Input: jewels = "aA", stones = "aAAbbbb"
Output: 3
```
**Example 2:**
```
Input: jewels = "z", stones = "ZZ"
Output: 0
```
## 解題想法:
* 此題為,判斷stones中哪些char符合jewels中的元素,求其總數
* 用set將jewls包起來再掃描stones逐一判斷即可
* c++: < set >
* 初始化: **set< xxx > obj_name (yyy.begin(), yyy.end());**
## Python:
``` python=
class Solution(object):
def numJewelsInStones(self, jewels, stones):
"""
:type jewels: str
:type stones: str
:rtype: int
"""
res=0
jew=set(jewels)
for item in stones:
if item in jew:
res+=1
return res
```
## C++:
``` cpp=
class Solution {
public:
int numJewelsInStones(string jewels, string stones) {
//init set
set<char> jew(jewels.begin(), jewels.end());
int res=0;
for (char item: stones){
if (jew.find(item)!=jew.end())
res+=1;
}
return res;
}
};
```