Posts Bulls and Cows (InterviewBit)
Post
Cancel

Bulls and Cows (InterviewBit)

PROBLEM DESCRIPTION

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:

The number of “bulls”, which are digits in the guess that are in the correct position. The number of “cows”, which are digits in the guess that are in your secret number but are located in the wrong position. Specifically, the non-bull digits in the guess that could be rearranged such that they become bulls.

Given the secret number secret and your friend’s guess guess, return the hint for your friend’s guess.

The hint should be formatted as “xAyB”, where x is the number of bulls and y is the number of cows. Note that both secret and guess may contain duplicate digits.

SOLUTION

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class Solution {

    public String solve(String A, String B) {

        int n = A.length(); // secret and guess length are same

        // freq map of secret string
        Map<Character, Integer> map = new HashMap<>();
        for(int i=0; i<n; i++){
            map.put(A.charAt(i), map.getOrDefault(A.charAt(i), 0) + 1);
        }

        int bull = 0;
        int cow = 0;

        // update the bulls if next char of A and B are same
        // reduce its frequency in map
        // if it becomes 0, remove it
        for(int  i=0; i<n; i++){

            char ch1 = A.charAt(i);
            char ch2 = B.charAt(i);

            if(ch1 == ch2){
                bull++;
                map.put(ch1, map.get(ch1)-1);

                if(map.get(ch1) == 0)
                    map.remove(ch1);
            }

        }


        // track cows now
        for(int i=0; i<n; i++){

            char ch1 = A.charAt(i);
            char ch2 = B.charAt(i);

            // if both are same, it would have been included in bulls already
            if(ch1 == ch2) continue;

            // otherwise check if it is present anywhere else
            // for that we can just check if it still exists in the map (which means it must have > 0 frequency)
            if(map.containsKey(ch2)){

                // increase cow count
                cow++;

                // reduce the freq of current char
                map.put(ch2, map.get(ch2)-1);

                // if it becomes 0, remove it
                if(map.get(ch2) == 0)
                    map.remove(ch2);

            }

        }


        return bull + "A" + cow + "B";

    }

}
This post is licensed under CC BY 4.0 by the author.