Bulls And Cows Solution for Google

Company: Google · Difficulty: Hard · Topics: String, Hash Table, Counting

Problem Statement

You are playing the Bulls and Cows game with your friend. You write down a secret number and ask your friend to guess what the number is. When your friend makes a guess, you provide a hint with the following info:

Key Understanding:

Examples

Example 1:

Input: secret = "1807", guess = "7810"

Output: "1A3B"

Explanation:

```

Secret: 1 8 0 7

Guess: 7 8 1 0

Result: C B C C (B=Bull, C=Cow)

```

Example 2:

Input: secret = "1123", guess = "0111"

Output: "1A1B"

Explanation:

```

Secret: 1 1 2 3

Guess: 0 1 1 1

Result: C C B C (B=Bull, C=Cow)

```

Example 3:

Input: secret = "1234", guess = "5678"

Output: "0A0B"

Explanation:

```

Secret: 1 2 3 4

Guess: 5 6 7 8

Result: X X X X (X=No match)

```

Example 4:

Input: secret = "1234", guess = "1234"

Output: "4A0B"

Explanation:

```

Secret: 1 2 3 4

Guess: 1 2 3 4

Result: B B B B (B=Bull)

```

Intuition

The key insight is to process the digits in two phases: first count bulls (exact matches), then count cows from the remaining unmatched digits.

Core Concept:

Why Two-Phase Approach Works:

Mathematical Insight:

Approach

Approach 1: Two-Pass Hash Table (Optimal)

Algorithm:

1. First pass: Count bulls and create frequency maps

2. For each position, if digits match, increment bulls

3. If digits don't match, add to frequency maps

4. Second pass: Count cows from frequency maps

5. For each digit in guess frequency map, add min(freq_secret, freq_guess) to cows

6. Return formatted string

Why This Works:

Complexity Analysis:

Approach 2: Single Pass with Array

Algorithm:

1. Use arrays to track digit frequencies

2. In single pass, count bulls and update frequencies

3. Calculate cows from frequency arrays

4. Return formatted string

Why This Works:

Complexity Analysis:

Approach 3: Brute Force

Algorithm:

1. Count bulls by comparing positions

2. For each unmatched digit in guess, check if it exists in secret

3. Count cows manually

4. Return formatted string

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: secret = "1807", guess = "7810"

Two-Pass Hash Table Approach:

Step 1: Initialize

Step 2: First pass - Count bulls

Step 3: Second pass - Count cows

Step 4: Return result

Single Pass Array Approach:

Step 1: Initialize arrays

Step 2: Single pass

Step 3: Calculate cows

Step 4: Return "1A3B"

Key Insights

1. Two-Phase Processing: Count bulls first, then cows

2. Frequency Tracking: Use arrays to track digit frequencies

3. Duplicate Handling: Each digit can only be used once

4. Position vs Value: Bulls care about position, cows only care about value

5. Minimum Frequency: Use min(freq1, freq2) to avoid double-counting

Edge Cases

1. Perfect Match: All digits are bulls

2. No Match: No bulls or cows

3. Duplicate Digits: Handle multiple occurrences of same digit

4. Single Digit: Strings of length 1

5. All Same Digits: Strings with all identical digits

6. Empty Strings: Handle edge cases (though constraints prevent this)

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Two-Pass Hash Table (Optimal)

Time Complexity: O(n) - Two passes through the strings

Space Complexity: O(1) - Fixed size arrays for digit frequencies

Advantages:

Disadvantages:

Solution 2: Single Pass with Array

Time Complexity: O(n) - Single pass through the strings

Space Complexity: O(1) - Fixed size arrays

Advantages:

Disadvantages:

Solution 3: Brute Force

Time Complexity: O(n²) - For each guess digit, scan secret

Space Complexity: O(1) - Constant extra space

Advantages:

Disadvantages:

More Google coding questions