Watch 10 video solutions for Best Poker Hand, a easy level problem involving Array, Hash Table, Counting. This walkthrough by Programming Live with Larry has 1,685 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"] Output: "Flush" Explanation: The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
Example 2:
Input: ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"] Output: "Three of a Kind" 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". Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand. Also note that other cards could be used to make the "Three of a Kind" hand.
Example 3:
Input: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"] Output: "Pair" Explanation: The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair". Note that we cannot make a "Flush" or a "Three of a Kind".
Constraints:
ranks.length == suits.length == 51 <= ranks[i] <= 13'a' <= suits[i] <= 'd'Problem Overview: You receive two arrays representing five playing cards: ranks and suits. The task is to determine the strongest possible poker hand from these cards. The possible outcomes are Flush, Three of a Kind, Pair, or High Card. The decision depends on whether all suits match or how frequently ranks repeat.
Approach 1: Using HashMap for Frequency Counting (O(n) time, O(n) space)
This approach counts how many times each rank appears using a hash map. First check if all suits are identical by comparing each suit with the first one. If they match, the answer is immediately Flush. Otherwise iterate through the ranks array and update a frequency map. The highest frequency determines the result: a count of 3 gives Three of a Kind, a count of 2 gives Pair, otherwise High Card. The algorithm performs a single pass through the cards, making it efficient and easy to implement with structures from Hash Table and Counting patterns.
Approach 2: Multiset for Simulation (O(n log n) time, O(n) space)
A multiset (or sorted multiset structure) stores card ranks while automatically keeping them ordered. Insert all five ranks into the multiset and then iterate through the container to measure consecutive duplicates. The longest duplicate streak determines whether you have a pair or three of a kind. Suit comparison is still handled separately by checking if every element in the suits array matches the first suit. Because insertion into a multiset typically costs O(log n), the total complexity becomes O(n log n). This approach mirrors how a real card hand might be evaluated and works well when you already rely on ordered containers.
Both methods operate on a fixed-size list of five cards, so the runtime is effectively constant in practice. However, the frequency-counting approach is conceptually cleaner and scales better if the problem size changes.
Recommended for interviews: The hash map counting approach is the expected solution. It demonstrates clear reasoning about frequency tracking and conditional evaluation while using standard Array traversal patterns. Interviewers mainly look for the quick flush check followed by rank frequency counting. The multiset approach works but adds unnecessary overhead for such a small dataset.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| HashMap for Frequency Counting | O(n) | O(n) | General case and interview-preferred approach for tracking rank frequencies efficiently |
| Multiset Simulation | O(n log n) | O(n) | Useful when working with ordered containers or when duplicates are easier to detect in sorted structures |