Carregando
LeetCode / 771. Jewels and Stones

Escolha uma linguagem de programação:

Confira o código-fonte da solução deste problema.

class Solution {
    // time O(n)
    // space O(n)
    public int numJewelsInStones(String jewels, String stones) {
        HashSet<Character> hashSet = new HashSet<Character>();
        int count = 0;

        for (int i = 0; i < jewels.length(); i++) {
            hashSet.add(jewels.charAt(i));
        }

        for (int i = 0; i < stones.length(); i++) {
            if (hashSet.contains(stones.charAt(i))) {
                count++;
            }
        }

        return count;
    }
}
public class Solution {
    public int NumJewelsInStones(string jewels, string stones) {
        HashSet<char> hashSet = new HashSet<char>();

        foreach (char c in jewels) {
            hashSet.Add(c);
        }

        int count = 0;

        foreach (char c in stones) {
            if (hashSet.Contains(c)) {
                count++;
            }
        }

        return count;
    }
}