← Home

299. Bulls and Cows

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
299

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 299. Bulls and Cows, the solution in this repository is mainly a greedy solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: greedy.

The notes already sitting in the source point us in the right direction:

  • use set: WA

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are getHint, tmp.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • 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. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

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//use set: WA
02class Solution {
03public:
04    string getHint(string secret, string guess) {
05        int N = secret.size();
06        int A = 0, B = 0;
07
08        for(int i = N-1; i >= 0; i--){
09            if(secret[i] == guess[i]){
10                A++;
11                secret.erase(i,1);
12                guess.erase(i,1);
13            }
14        }
15
16        //use set: WA
17        set<char> s1(secret.begin(), secret.end()), s2(guess.begin(), guess.end());
18        vector<char> tmp(4);
19        B = set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), tmp.begin()) - tmp.begin();
20        
21        return to_string(A) + "A" + to_string(B) + "B";
22    }
23};
24
25//Runtime: 8 ms, faster than 57.55% of C++ online submissions for Bulls and Cows.
26//Memory Usage: 7.9 MB, less than 100.00% of C++ online submissions for Bulls and Cows.
27class Solution {
28public:
29    string getHint(string secret, string guess) {
30        int N = secret.size();
31        int A = 0, B = 0;
32
33        for(int i = N-1; i >= 0; i--){
34            if(secret[i] == guess[i]){
35                A++;
36                secret.erase(i,1);
37                guess.erase(i,1);
38            }
39        }
40        
41        sort(secret.begin(), secret.end());
42        sort(guess.begin(), guess.end());
43        int i = 0, j = 0;
44        while(i < secret.size() && j < secret.size()){
45            if(secret[i] == guess[j]){
46                B++;
47                i++;
48                j++;
49            }else if(secret[i] < guess[j]){
50                i++;
51            }else{
52                j++;
53            }
54        }
55        
56        return to_string(A) + "A" + to_string(B) + "B";
57    }
58};

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.