![](https://img-blog.csdnimg.cn/8139f528f7fd4f74b2a3c4729244237a.png)
![](https://img-blog.csdnimg.cn/bdae9116ac664e3990c28d76397f56c7.png)
![](https://img-blog.csdnimg.cn/ef8c0a40b0a84e258071a96ce900fd72.png)
class Solution {
public:
int numJewelsInStones(string jewels, string stones) {
int jewelsCount = 0;
int jewelsLength = jewels.length(), stonesLength = stones.length();
for (int i = 0; i < stonesLength; i++) {
char stone = stones[i];
for (int j = 0; j < jewelsLength; j++) {
char jewel = jewels[j];
if (stone == jewel) {
jewelsCount++;
break;
}
}
}
return jewelsCount;
}
};
![](https://img-blog.csdnimg.cn/2cd1a33c141b43b9a1905626ec19a141.png)
class Solution {
public:
int numJewelsInStones(string jewels, string stones) {
int jewelsCount = 0;
unordered_set<char> jewelsSet;
int jewelsLength = jewels.length(), stonesLength = stones.length();
for (int i = 0; i < jewelsLength; i++) {
char jewel = jewels[i];
jewelsSet.insert(jewel);
}
for (int i = 0; i < stonesLength; i++) {
char stone = stones[i];
if (jewelsSet.count(stone)) {
jewelsCount++;
}
}
return jewelsCount;
}
};
![](https://img-blog.csdnimg.cn/cbef5b1c54d040cc9de98a2a9286493e.png)