|
| 1 | +/** |
| 2 | + * [2347] Best Poker Hand |
| 3 | + * |
| 4 | + * You are given an integer array ranks and a character array suits. You have 5 cards where the i^th card has a rank of ranks[i] and a suit of suits[i]. |
| 5 | + * The following are the types of poker hands you can make from best to worst: |
| 6 | + * <ol> |
| 7 | + * "Flush": Five cards of the same suit. |
| 8 | + * "Three of a Kind": Three cards of the same rank. |
| 9 | + * "Pair": Two cards of the same rank. |
| 10 | + * "High Card": Any single card. |
| 11 | + * </ol> |
| 12 | + * Return a string representing the best type of poker hand you can make with the given cards. |
| 13 | + * Note that the return values are case-sensitive. |
| 14 | + * |
| 15 | + * Example 1: |
| 16 | + * |
| 17 | + * Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"] |
| 18 | + * Output: "Flush" |
| 19 | + * Explanation: The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush". |
| 20 | + * |
| 21 | + * Example 2: |
| 22 | + * |
| 23 | + * Input: ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"] |
| 24 | + * Output: "Three of a Kind" |
| 25 | + * Explanation: The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind". |
| 26 | + * Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand. |
| 27 | + * Also note that other cards could be used to make the "Three of a Kind" hand. |
| 28 | + * Example 3: |
| 29 | + * |
| 30 | + * Input: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"] |
| 31 | + * Output: "Pair" |
| 32 | + * Explanation: The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair". |
| 33 | + * Note that we cannot make a "Flush" or a "Three of a Kind". |
| 34 | + * |
| 35 | + * |
| 36 | + * Constraints: |
| 37 | + * |
| 38 | + * ranks.length == suits.length == 5 |
| 39 | + * 1 <= ranks[i] <= 13 |
| 40 | + * 'a' <= suits[i] <= 'd' |
| 41 | + * No two cards have the same rank and suit. |
| 42 | + * |
| 43 | + */ |
| 44 | +pub struct Solution {} |
| 45 | + |
| 46 | +// problem: https://leetcode.com/problems/best-poker-hand/ |
| 47 | +// discuss: https://leetcode.com/problems/best-poker-hand/discuss/?currentPage=1&orderBy=most_votes&query= |
| 48 | + |
| 49 | +// submission codes start here |
| 50 | + |
| 51 | +impl Solution { |
| 52 | + pub fn best_hand(ranks: Vec<i32>, suits: Vec<char>) -> String { |
| 53 | + match suits.iter().collect::<std::collections::HashSet<_>>().len() == 1 { |
| 54 | + true => "Flush".to_string(), |
| 55 | + false => { |
| 56 | + let mut counter = [0; 14]; |
| 57 | + ranks.iter().for_each(|&r| counter[r as usize] += 1); |
| 58 | + match counter.into_iter().max().unwrap() { |
| 59 | + 1 => "High Card".to_string(), |
| 60 | + 2 => "Pair".to_string(), |
| 61 | + _ => "Three of a Kind".to_string(), |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// submission codes end |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn test_2347_example_1() { |
| 76 | + let ranks = vec![13, 2, 3, 1, 9]; |
| 77 | + let suits = vec!['a', 'a', 'a', 'a', 'a']; |
| 78 | + |
| 79 | + let result = "Flush".to_string(); |
| 80 | + |
| 81 | + assert_eq!(Solution::best_hand(ranks, suits), result); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_2347_example_2() { |
| 86 | + let ranks = vec![4, 4, 2, 4, 4]; |
| 87 | + let suits = vec!['d', 'a', 'a', 'b', 'c']; |
| 88 | + |
| 89 | + let result = "Three of a Kind".to_string(); |
| 90 | + |
| 91 | + assert_eq!(Solution::best_hand(ranks, suits), result); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_2347_example_3() { |
| 96 | + let ranks = vec![10, 10, 2, 12, 9]; |
| 97 | + let suits = vec!['a', 'b', 'c', 'a', 'd']; |
| 98 | + |
| 99 | + let result = "Pair".to_string(); |
| 100 | + |
| 101 | + assert_eq!(Solution::best_hand(ranks, suits), result); |
| 102 | + } |
| 103 | +} |
0 commit comments