Jewels And Stones Problem


Description

LeetCode Problem 771. 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”. Jewels and stones consist of only English letters.

Example 1:

1
2
Input: jewels = "aA", stones = "aAAbbbb"
Output: 3

Example 2:

1
2
Input: jewels = "z", stones = "ZZ"
Output: 0


Solution

When scanning through the jewels, we can use a hash table to store the jewels. Then for each stone, we check whether it matches any of the jewels.

To speed up the program, we can leverage the constraint that jewels and stones consist of only English letters. We can use an array instead of a hash table, and use the ASCII number of the letter as the index (instead of using the letter itself).

The time complexity is O(n + m), where n is the number of jewels and m is the number of stones. The space complexity is O(n).


Sample C++ Code

This is a C++ implementation of the above approach.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int numJewelsInStones(string J, string S) {
    vector<int> ht(100, 0);
    for (int i = 0; i < J.size(); i ++) {
        ht[J[i]-'A'] = 1;
    }
    int cnt = 0;
    for (int i = 0; i < S.size(); i ++) {
        if (ht[S[i]-'A'] == 1)
            cnt ++;
    }
    return cnt;
}

int main() {
    string J = "aA";
    string S = "aAAbbbb";
    cout << numJewelsInStones(J, S) << endl;
}




Related Posts

X Of A Kind In A Deck Of Cards Problem

LeetCode 914. In a deck of cards, each card has...

Word Subsets Problem

LeetCode 916. You are given two string arrays words1 and...

Vowel Spellchecker Problem

LeetCode 966. Given a wordlist, we want to implement a...

Verifying An Alien Dictionary Problem

LeetCode 953. In an alien language, surprisingly, they also use...

Unique Morse Code Words Problem

LeetCode 804. International Morse Code defines a standard encoding where...

Unique Email Addresses Problem

LeetCode 929. Every valid email consists of a local name...