给定字符串J
代表石头中宝石的类型,和字符串 S
代表你拥有的石头。 S
中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
J
中的字母不重复,J
和 S
中的所有字符都是字母。字母区分大小写,因此"a"
和"A"
是不同类型的石头。
示例 :
输入: J = "aA", S = "aAAbbbb"
输出: 3
暴力搜索法
class Solution {
public int numJewelsInStones(String J, String S) {
int count = 0;
for (int i = 0; i < J.length(); i++) {
for (int j = 0; j < S.length(); j++) {
if (J.charAt(i) == S.charAt(j)) {
count++;
}
}
}
return count;
}
}
哈希集合法
class Solution {
public int numJewelsInStones(String J, String S) {
int count = 0;
Set<Character> set = new HashSet<>();
for (char c:J.toCharArray()) {
set.add(c);
}
for (char c:S.toCharArray()) {
if (set.contains(c)) {
count++;
}
}
return count;
}
}