LeetCode Problem Workspace

Best Poker Hand

Determine the strongest poker hand from five cards using array scanning and hash table counting techniques efficiently.

category

3

Topics

code_blocks

7

Code langs

hub

3

Related

Practice Focus

Easy · Array scanning plus hash lookup

bolt

Answer-first summary

Determine the strongest poker hand from five cards using array scanning and hash table counting techniques efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array scanning plus hash lookup

Try AiBox Copilotarrow_forward

Start by checking if all suits are identical to identify a Flush. If not, count the frequency of ranks to detect Three of a Kind or Pair. Sequentially returning the first satisfied condition ensures correct selection of the highest possible hand using array scanning plus hash lookup.

Problem Statement

You are given two arrays: ranks, an integer array of length 5 representing the ranks of your cards, and suits, a character array of length 5 representing the suits. Each card is uniquely defined by its rank and suit.

Determine the best poker hand achievable with these cards. The possible hands, ranked from strongest to weakest, are: Flush, Three of a Kind, Pair, and High Card. Return the corresponding string for the strongest hand.

Examples

Example 1

Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]

Output: "Flush"

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"

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"

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.

Solution Approach

Check for Flush using array scan

Iterate through the suits array to verify if all five suits are the same. If true, immediately return 'Flush' since it is the strongest hand. This uses direct array scanning and a simple hash check for early exit.

Count ranks with hash table

Create a hash map to record the frequency of each rank. Scan ranks and increment counts. After scanning, check for counts of 3 to identify 'Three of a Kind', then check for counts of 2 to identify 'Pair'. This ensures correct detection of the next strongest hand after Flush.

Return default High Card

If neither a Flush nor any repeated ranks are found, return 'High Card' as the default result. This handles all remaining cases and ensures the solution covers every possible input hand using array scanning plus hash lookup.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(5) for scanning suits and O(5) for counting ranks, effectively constant due to fixed array size. Space complexity is O(1) as the rank hash map stores at most 13 entries.

What Interviewers Usually Probe

  • Look for early exit on Flush condition before counting ranks.
  • Check rank frequencies carefully; interviewer may ask about ties between Pair and Three of a Kind.
  • Consider edge cases where multiple hands satisfy conditions and confirm strongest hand is selected.

Common Pitfalls or Variants

Common pitfalls

  • Checking rank counts before verifying Flush can return incorrect lower hand.
  • Ignoring that multiple cards with same rank can form different hand combinations.
  • Overcomplicating with sorting when simple counting and array scan suffices.

Follow-up variants

  • Determine best hand for 7-card poker using the same array scanning plus hash lookup pattern.
  • Adjust to handle repeated ranks in non-unique card decks, updating hash counting logic.
  • Identify second-best hand if strongest hand is removed, requiring conditional checks in order.

FAQ

What is the best approach to solve Best Poker Hand problem?

Use array scanning to check Flush first, then count ranks with a hash map to find Three of a Kind or Pair.

Why does order of condition checks matter?

Because Flush is stronger than Three of a Kind or Pair, checking it first ensures the highest hand is returned.

Can I solve this without a hash map?

Yes, but using a hash map simplifies counting rank frequencies and avoids repeated scans of the array.

What is the time complexity of this solution?

Time complexity is effectively O(1) since arrays are fixed length 5; space complexity is O(1) for the rank map.

How does array scanning plus hash lookup pattern apply here?

You scan suits for Flush and use a hash lookup to count ranks, directly applying this pattern to efficiently determine the best hand.

terminal

Solution

Solution 1: Counting

We first traverse the array $\textit{suits}$ to check if adjacent elements are equal. If they are, we return `"Flush"`.

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def bestHand(self, ranks: List[int], suits: List[str]) -> str:
        # if len(set(suits)) == 1:
        if all(a == b for a, b in pairwise(suits)):
            return 'Flush'
        cnt = Counter(ranks)
        if any(v >= 3 for v in cnt.values()):
            return 'Three of a Kind'
        if any(v == 2 for v in cnt.values()):
            return 'Pair'
        return 'High Card'
Best Poker Hand Solution: Array scanning plus hash lookup | LeetCode #2347 Easy