보석인 돌의 종류를 나타내는 줄 보석과 가지고 있는 돌을 나타내는 줄이 주어집니다. 돌 속의 각 캐릭터는 당신이 가지고 있는 돌의 종류입니다. 당신은 얼마나 많은 돌들이 보석인지 알고 싶어합니다.
글자는 대소문자를 구분하므로, "a"는 "A"와 다른 종류의 돌로 간주된다.
https://leetcode.com/problems/jewels-and-stones/
You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.
Letters are case sensitive, so "a" is considered a different type of stone from "A".
Example 1:
Input: jewels = "aA", stones = "aAAbbbb"
Output: 3
Example 2:
Input: jewels = "z", stones = "ZZ"
Output: 0
Constraints:
1 <= jewels.length, stones.length <= 50
jewels and stones consist of only English letters.
All the characters of jewels are unique.
자바입니다.
HashSet
class Solution {
public int numJewelsInStones(String jewels, String stones) {
Set<Character> jewel = new HashSet<>();
for(char i:jewels.toCharArray()){
jewel.add(i);
}
int count=0;
for(char i:stones.toCharArray()){
if(jewel.contains(i)) count++;
}
return count;
}
}
indexOf
class Solution {
public int numJewelsInStones(String jewels, String stones) {
int count=0;
for(int i=0; i<stones.length();i++){
if(jewels.indexOf(stones.charAt(i))!=-1) count++;
}
return count;
}
}