Skip to main content

Best Poker Hand - Solution & Explanation

EasyArrayHash TableCounting20 min readAsked at: Amazon, Apple
Practice this problem

Problem Statement

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:

  1. "Flush": Five cards of the same suit.
  2. "Three of a Kind": Three cards of the same rank.
  3. "Pair": Two cards of the same rank.
  4. "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 == 5
  • 1 <= ranks[i] <= 13
  • 'a' <= suits[i] <= 'd'
  • No two cards have the same rank and suit.

Approach Overview

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 1: Using HashMap for Frequency Counting

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Multiset for Simulation

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N) concentrating on efficient counting with frequency arrays.
Space Complexity: O(1) due to the constant size arrays utilized.

Try this approach in the editor →

Approach 3: Counting

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:

  • If any card appears 3 times, return "Three of a Kind";
  • Otherwise, if any card appears 2 times, return "Pair";
  • Otherwise, return "High Card".

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array ranks.

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using HashMap for Frequency Counting

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.

Multiset for Simulation

Time Complexity: O(N) concentrating on efficient counting with frequency arrays.
Space Complexity: O(1) due to the constant size arrays utilized.

Counting—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
HashMap for Frequency CountingO(n)O(n)General case and interview-preferred approach for tracking rank frequencies efficiently
Multiset SimulationO(n log n)O(n)Useful when working with ordered containers or when duplicates are easier to detect in sorted structures

Video Solution

2347. Best Poker Hand (Leetcode Easy) • Programming Live with Larry • 1,704 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Best Poker Hand easy or hard?
Best Poker Hand is categorized as an Easy problem on LeetCode with an acceptance rate above 60%. It mainly tests simple array traversal and frequency counting rather than complex algorithms.
Best Poker Hand Python/Java solution
Python solutions typically use a dictionary or collections.Counter to track rank frequencies. Java implementations rely on HashMap or an integer array of size 13 for counting ranks. Both implementations follow the same logic: check for flush first, then evaluate the maximum rank frequency.
How to solve Best Poker Hand in O(n)?
Iterate through the suits array to verify whether every suit matches the first one. If they all match, return "Flush" immediately. Otherwise build a frequency map of ranks and track the maximum occurrence. A frequency of 3 means Three of a Kind, 2 means Pair, and otherwise the hand is High Card.
What is the best approach for Best Poker Hand?
The most efficient approach uses a hash map to count the frequency of each rank while also checking if all suits are identical. First verify whether all suits match to detect a Flush. If not, compute rank frequencies to determine whether a Three of a Kind, Pair, or High Card exists. This method runs in O(n) time with O(n) space.
Is Best Poker Hand asked at Google/Amazon/Meta?
This problem targets fundamental array and counting patterns commonly used in coding interviews. Variations involving frequency counting and card evaluation appear in interview question banks used by companies such as Amazon and Google for entry-level screening rounds.
What data structure is used in Best Poker Hand?
The primary data structure is a hash map (or dictionary) used to store the frequency of each card rank. This allows constant-time updates and quick identification of duplicate ranks. Arrays are also used for iterating through suits and ranks.
What is the time complexity of Best Poker Hand?
The optimal solution runs in O(n) time where n is the number of cards. Each card is visited once to check suits and update rank frequencies. Since the problem always contains five cards, the runtime behaves like constant time in practice.

Ready to solve this problem?

Practice Best Poker Hand with our built-in code editor and test cases.

Practice on FleetCode