Bulls and Cows
Brute Force Approach
- Mark and count all - bullsi.e. matched chars and indices.
- Mark and count all those not marked as - bulls
// function to count number of bulls and mark each
// bull char in input as used. 
int getBullsAndMark(string &A, const string &B) {
    int count = 0;
    for(int i = 0; i < A.size(); i++)    
        if(A[i] == B[i]) {
            A[i] = 'b';
            count++;
        }
        
    return count;
}
// function to find and mark ch in A as used as cow
bool MarkIfUnused(string &A, const char &ch) {
    for(int i = 0; i < A.size(); i++)
        if(A[i] == ch) {
            A[i] = 'c';
            return true;
        }
        
        return false;
}
string Solution::solve(string A, string B) {
    int bulls = getBullsAndMark(A, B), cows = 0;
    
    // if unused cow found then count for each
    // ununsed in B 
    // Use A[i] == 'b' => B[i] == 'b'
    for(int i = 0; i < B.size(); i++) 
        if(A[i] != 'b' && MarkIfUnused(A, B[i]))
            cows++;
            
    return to_string(bulls) + "A" + to_string(cows) + "B";    
}Time Complexity: 
Space Complexity: 
Last updated