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 Sis 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.

解题思路

此题难度为easy,遍历S中每个letter s并判断s是否在J中存在,若存在个数加一。

 

代码

 1 class Solution {
 2 public:
 3     int numJewelsInStones(string J, string S) {
 4         int num = 0;
 5         for (auto s : S) {
 6             if (J.find(s) != string::npos)
 7                 num++;
 8         }
 9         return num;
10     }
11 };

 

posted @ 2018-07-26 19:27  gszzsg  阅读(101)  评论(0编辑  收藏  举报