|
| 1 | +/** |
| 2 | + * [299] Bulls and Cows |
| 3 | + * |
| 4 | + * You are playing the following <a href="https://en.wikipedia.org/wiki/Bulls_and_Cows" target="_blank">Bulls and Cows</a> game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number. |
| 5 | + * |
| 6 | + * Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. |
| 7 | + * |
| 8 | + * Please note that both secret number and friend's guess may contain duplicate digits. |
| 9 | + * |
| 10 | + * Example 1: |
| 11 | + * |
| 12 | + * |
| 13 | + * Input: secret = "1807", guess = "7810" |
| 14 | + * |
| 15 | + * Output: "1A3B" |
| 16 | + * |
| 17 | + * Explanation: 1<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> bull and </span>3<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> cows. The bull is </span>8<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">, the cows are </span>0<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">, </span>1<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> and </span>7<font face="sans-serif, Arial, Verdana, Trebuchet MS">.</font> |
| 18 | + * |
| 19 | + * Example 2: |
| 20 | + * |
| 21 | + * |
| 22 | + * Input: secret = "1123", guess = "0111" |
| 23 | + * |
| 24 | + * Output: "1A1B" |
| 25 | + * |
| 26 | + * Explanation: The 1st 1 <span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">in friend's guess is a bull, the 2nd or 3rd </span>1<span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";"> is a cow</span><span style="font-family: sans-serif, Arial, Verdana, "Trebuchet MS";">.</span> |
| 27 | + * |
| 28 | + * Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal. |
| 29 | + */ |
| 30 | +pub struct Solution {} |
| 31 | + |
| 32 | +// submission codes start here |
| 33 | + |
| 34 | +impl Solution { |
| 35 | + pub fn get_hint(secret: String, guess: String) -> String { |
| 36 | + let mut bulls = 0; |
| 37 | + let mut cows = 0; |
| 38 | + let mut s_iter = secret.chars(); |
| 39 | + let mut g_iter = guess.chars(); |
| 40 | + let mut bucket = vec![0; 10]; |
| 41 | + let mut non_match = vec![]; |
| 42 | + while let (Some(s), Some(g)) = (s_iter.next(), g_iter.next()) { |
| 43 | + if s == g { |
| 44 | + bulls += 1; |
| 45 | + } else { |
| 46 | + bucket[s.to_digit(10).unwrap() as usize] += 1; |
| 47 | + non_match.push(g.to_digit(10).unwrap() as usize); |
| 48 | + } |
| 49 | + } |
| 50 | + for &idx in non_match.iter() { |
| 51 | + if bucket[idx] > 0 { |
| 52 | + cows += 1; |
| 53 | + bucket[idx] -= 1; |
| 54 | + } |
| 55 | + } |
| 56 | + format!("{}A{}B", bulls, cows) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// submission codes end |
| 61 | + |
| 62 | +#[cfg(test)] |
| 63 | +mod tests { |
| 64 | + use super::*; |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn test_299() { |
| 68 | + assert_eq!(Solution::get_hint("1807".to_owned(), "7810".to_owned()), "1A3B".to_owned()); |
| 69 | + assert_eq!(Solution::get_hint("1123".to_owned(), "0111".to_owned()), "1A1B".to_owned()); |
| 70 | + } |
| 71 | +} |
0 commit comments