Q:
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.
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
andJ
will consist of letters and have length at most 50.- The characters in
J
are distinct.
Analysis:
此题的题意为给定字符串J为含有宝石的字符串,J中每个字符都可以成为一个宝石,S字符串是普通给定的石头,要查找出S中含有宝石的个数,即含有J中字符的个数,提示中说明J中字符均不相同,同时区分大小写。
解题思路:
直接遍历字符串S中的每个字符,同时判断如果该字符在J中出现,则S中含有宝石的数量+1,遍历完S后,即可得到结果,并将结果返回。
Code:
class Solution {
public int numJewelsInStones(String J, String S) {
// 统计出现的次数
int count=0;
// 将S遍历每个字符,查看String J中是否含有S的字符
for(int i=0;i<S.length();i++){
char c=S.charAt(i);
String c1=String.valueOf(c);
if(J.contains(c1)){
// 含有的话数量+1
count++;
}
}
return count;
}
}