← Home

771. Jewels and Stones

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
771

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 771. Jewels and Stones, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are numJewelsInStones.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels.
03
04The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".
05
06Example 1:
07
08Input: J = "aA", S = "aAAbbbb"
09Output: 3
10Example 2:
11
12Input: J = "z", S = "ZZ"
13Output: 0
14Note:
15
16S and J will consist of letters and have length at most 50.
17The characters in J are distinct.
18**/
19
20/**
21You are here! 
22Your runtime beats 98.58 % of cpp submissions.
23**/
24
25class Solution {
26public:
27    int numJewelsInStones(string J, string S) {
28        int count = 0;
29        for(int i=0; i < J.length(); i++){
30            //find J's one character in S
31            //search from the head of S
32            std::size_t found = 0;
33            
34            while(true){
35                found = S.find(J.at(i), found);
36                if(found!=std::string::npos){
37                    count+=1;
38                }else{
39                    break;
40                }
41                //next time, start from the next character
42                found+=1;
43            }
44        }
45        return count;
46    }
47};
48
49//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Jewels and Stones.
50//Memory Usage: 6.3 MB, less than 100.00% of C++ online submissions for Jewels and Stones.
51class Solution {
52public:
53    int numJewelsInStones(string J, string S) {
54        int ans = 0;
55        for(char s : S){
56            if(J.find(string(1, s)) != string::npos){
57                ans++;
58            }
59        }
60        return ans;
61    }
62};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.