CodingTest/LeetCode

[LeetCode] 771. Jewels and Stones, Easy

뜸부깅 2025. 4. 9. 14:29
반응형

1. 문제

  • 문자열 jwels, stones가 주어질 때, stones에 포함되어 있는 jweles의 개수를 반환하라.

2. 해결

function numJewelsInStones(jewels: string, stones: string): number {
    const set = new Set<string>(jewels);
    
    let count = 0;
    
    for(const stone of stones.split('')){
        if(set.has(stone)) count++;
    }
    
    return count;
};