/** 299. Bulls and Cows * 2016-7-2 by Mingyang * code is ugly, but this topic is too simple, not details * Special case 1122-"1222 * guess the first 2 in the show will let me in the CO W plus 1 * So I'm going to go through the equal so it won't be wrong, but the code can be modified to a lot of optimizations, there's time to write slowly*/ Public Staticstring Gethint (String secret, String guess) {intBull = 0; intCow = 0; HashSet<Character> set =NewHashset<character>(); int[] Array =New int[10]; for(inti = 0; I < secret.length (); i++) { CharA =Secret.charat (i); Array[a-' 0 ']++; Set.add (a); } for(inti = 0; I < guess.length (); i++) { Charb =Guess.charat (i); if(Guess.charat (i) = =Secret.charat (i)) {Bull++; Array[b-' 0 ']--; Continue; } } for(inti = 0; I < guess.length (); i++) { Charb =Guess.charat (i); if(Set.contains (b) && Guess.charat (i)! =Secret.charat (i)) { if(Array[b-' 0 ') > 0) {Cow++; Array[b-' 0 ']--; }}} String res= Bull + "A" + Cow + "B"; returnRes; }
299. Bulls and cows