771. Jewels and Stones

양성준·2025년 4월 29일

코딩테스트

목록 보기
36/102

문제

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

풀이

class Solution {
    public int numJewelsInStones(String jewels, String stones) {
        Map<Character, Integer> map = new HashMap<>();
        int answer = 0;

        for(char c : stones.toCharArray()) {
            map.put(c, map.getOrDefault(c,0) + 1);
        }

        for (char c : jewels.toCharArray()) {
            answer += map.getOrDefault(c, 0);
        }

        return answer;
        
    }
}
profile
백엔드 개발자

0개의 댓글