algoadvance

Leetcode 2347. Best Poker Hand

Problem Statement

Alice has a hand of 5 cards, which might only contain integers from 1 to 13 (inclusive). The cards can be represented as an integer array where each value represents the card’s face value.

Alice needs to determine the best possible poker hand that can be made with these 5 cards.

The possible poker hands are:

  1. “Four of a Kind”: Four cards with the same face value.
  2. “Full House”: Three cards of one face value and two cards of another face value.
  3. “Three of a Kind”: Three cards with the same face value.
  4. “Two Pair”: Two different pairs of cards.
  5. “Pair”: Two cards with the same face value.
  6. “High Card”: Any hand that does not meet the above conditions.

Write a function to determine the best poker hand Alice can have.

Example:

Input: cards = [4,4,4,4,5]
Output: "Four of a Kind"

Input: cards = [10,10,10,4,4]
Output: "Full House"

Clarifying Questions

  1. Can the cards have any integer value other than 1 to 13?
    • No, the cards will only have values from 1 to 13.
  2. Will the cards always contain exactly 5 cards?
    • Yes, the input will always be an array of 5 integers.
  3. Should the function consider the suit of the cards?
    • No, only the face values matter for this problem.

Strategy

  1. First, count the occurrences of each face value using a hash map.
  2. Based on the counts:
    • Check if any count == 4 for “Four of a Kind”.
    • Check if the counts contain both a 3 and a 2 for “Full House”.
    • Check if any count == 3 for “Three of a Kind”.
    • Check if there are exactly two pairs for “Two Pair”.
    • Check if any count == 2 for “Pair”.
  3. If none of the above conditions meet, return “High Card”.

Code

import java.util.HashMap;
import java.util.Map;

public class BestPokerHand {
    public String bestHand(int[] cards) {
        Map<Integer, Integer> countMap = new HashMap<>();
        for (int card : cards) {
            countMap.put(card, countMap.getOrDefault(card, 0) + 1);
        }
        
        boolean hasThree = false, hasPair = false;
        int pairs = 0;
        
        for (int count : countMap.values()) {
            if (count == 4) {
                return "Four of a Kind";
            } else if (count == 3) {
                hasThree = true;
            } else if (count == 2) {
                hasPair = true;
                pairs++;
            }
        }
        
        if (hasThree && hasPair) {
            return "Full House";
        } else if (hasThree) {
            return "Three of a Kind";
        } else if (pairs == 2) {
            return "Two Pair";
        } else if (pairs == 1) {
            return "Pair";
        }
        
        return "High Card";
    }

    public static void main(String[] args) {
        BestPokerHand pokerHand = new BestPokerHand();
        System.out.println(pokerHand.bestHand(new int[]{4, 4, 4, 4, 5})); // Four of a Kind
        System.out.println(pokerHand.bestHand(new int[]{10, 10, 10, 4, 4})); // Full House
        System.out.println(pokerHand.bestHand(new int[]{2, 4, 4, 5, 6})); // Pair
        System.out.println(pokerHand.bestHand(new int[]{1, 2, 3, 4, 5})); // High Card
    }
}

Time Complexity

The time complexity of this solution is O(n), where n is the number of cards. Given that n is always 5 in this problem, our algorithm runs in constant time, O(1).

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI