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.
This approach involves using a hash map to count frequencies of ranks and suits. First, check if all suits are the same for a Flush. Then, analyze the frequency map of ranks to determine if there's a Three of a Kind or a Pair.
This C program initializes arrays to count frequency of ranks. It checks for a flush by comparing each suit to the first suit. If not a Flush, it checks the frequency array for Three of a Kind or Pair. Lastly, it returns the best possible hand.
Time Complexity: O(N), where N is the number of cards, because we iterate over them twice.
Space Complexity: O(1), as the space used by the frequency array does not change with input size.
This approach simulates poker hand ranking using a multiset-like logic to efficiently handle frequency counts. The focus is on directly counting occurrences of suits and ranks to determine the best poker hand.
Using arrays to count suit and rank occurrences, this C solution efficiently checks for Flush, Three of a Kind, and Pair, much like a multiset would. The focus is placed on counting and comparison logic to classify the hand type.
Time Complexity: O(N) concentrating on efficient counting with frequency arrays.
Space Complexity: O(1) due to the constant size arrays utilized.
We first traverse the array suits to check if adjacent elements are equal. If they are, we return "Flush".
Next, we use a hash table or array cnt to count the quantity of each card:
3 times, return "Three of a Kind";2 times, return "Pair";"High Card".The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array ranks.
| Approach | Complexity |
|---|---|
| Using HashMap for Frequency Counting | Time Complexity: O(N), where N is the number of cards, because we iterate over them twice. |
| Multiset for Simulation | Time Complexity: O(N) concentrating on efficient counting with frequency arrays. |
| Counting | — |
| 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 |
2347. Best Poker Hand (Leetcode Easy) • Programming Live with Larry • 1,685 views views
Watch 9 more video solutions →Practice Best Poker Hand with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor