Jewels and Stones

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".

LeetCode Problem - 771

class Solution {
    // Function to count the number of jewels in stones
    public int numJewelsInStones(String jewels, String stones) {
        int result = 0; // Initialize the result counter

        // Iterate over each jewel character
        for(char j : jewels.toCharArray()){
            // Iterate over each stone character
            for(char s : stones.toCharArray()){
                if(j == s){ // If the jewel matches the stone
                    result++; // Increment the result counter
                }
            }
        }
        return result; // Return the total count of jewels in stones
    }
}