Algorithm/LeetCode

[hash] jewels and stones

sw_develop 2025. 4. 8. 09:52

 Problem

https://leetcode.com/problems/jewels-and-stones/description/

 

 Approach & Solution

방식) HashSet 사용

더보기
class Solution {
    public int numJewelsInStones(String jewels, String stones) {
        int cnt = 0;
        
        Set<Character> jSet = new HashSet<>();
        for (Character c : jewels.toCharArray()) {
            jSet.add(c);
        }

        for (Character c : stones.toCharArray()) {
            if (jSet.contains(c)) {
                cnt += 1;
            }
        }
        
        return cnt;
    }
}