#2347
Easy Algorithms Best poker hand
Array Hash Table Counting
61.6% acceptance
Feb 25, 2026
402
44
You are given an integer array ranks and a character array suits. You have 5 cards where the ith card has a rank of ranks[i] and a suit of suits[i].
The following are the types of poker hands you can make from best to worst:
"Flush": Five cards of the same suit.
"Three of a Kind": Three cards of the same rank.
"Pair": Two cards of the same rank.
"High Card": Any single card.
Return a string representing the best type of poker hand you can make with the given cards.
Note that the return values are case-sensitive.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn best_hand(ranks: Vec<i32>, suits: Vec<char>) -> String {
if suits.iter().all(|&s| s == suits[0]) { return "Flush".to_string(); }
let mut freq = [0i32; 14];
for &r in &ranks { freq[r as usize] += 1; }
let max_freq = *freq.iter().max().unwrap();
if max_freq >= 3 { return "Three of a Kind".to_string(); }
if max_freq >= 2 { return "Pair".to_string(); }
"High Card".to_string()
}
}