Secret and guess are read in bits, and if equal bull is added 1, the inequality is in the array of each number of different numbers in which they are counted, plus 1 (s_map[i] refers to the number of numbers I appear in secret). The last S_map and G_map are the number of occurrences of each value in the respective string, cow equals the smallest of the same position in two arrays.
1 classSolution {2 Public:3 stringGethint (stringSecretstringguess) {4 ints_map[Ten] = {0}, g_map[Ten] = {0};5 intBull =0, cow =0;6 7 for(inti =0; I < secret.length (); ++i) {8 if(Secret[i] = = Guess[i]) + +Bull;9 Else{Ten++s_map[secret[i]-'0']; One++g_map[guess[i]-'0']; A } - } - the for(inti =0; I <Ten; ++i) { -Cow + =min (s_map[i], g_map[i]); - } - + - stringres =""; +res = to_string (Bull) +"A"+ to_string (Cow) +"B"; A returnRes; at } -};
Leetcode 299. Bulls and cows