Jewels and Stones

Posted gsz-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 };

 

以上是关于Jewels and Stones的主要内容,如果未能解决你的问题,请参考以下文章

Jewels and Stones

LeetCode 771. Jewels and Stones

LeetCode 771. Jewels and Stones

[leetcode-771-Jewels and Stones]

Leetcode #771. Jewels and Stones

771. Jewels and Stones - Easy