Similar Problems

Similar Problems not available

Best Poker Hand - Leetcode Solution

Companies:

LeetCode:  Best Poker Hand Leetcode Solution

Difficulty: Easy

Topics: hash-table array  

The Best Poker Hand problem on LeetCode is a simple problem that requires you to identify the best poker hand from a given set of cards. The problem statement provides you with a list of cards each having a rank and a suit, and your task is to find the best hand this list of cards can form.

Here are the steps involved in solving the Best Poker Hand problem on LeetCode:

  1. Create a dictionary to map each rank to its corresponding integer value. For example, 'A', '2', '3', '4', ..., 'J', 'Q', 'K' can be mapped to 14, 2, 3, 4, ..., 11, 12, 13 respectively.

  2. Create a list of tuples where each tuple contains the rank and suit of a card. For example, [('2', 'H'), ('3', 'D'), ('4', 'C'), ('5', 'D'), ('6', 'H')].

  3. Sort the list of tuples in descending order of card ranks.

  4. Check for the presence of different poker hands in the sorted list of tuples in the following order of importance: a. Royal flush: Ace, King, Queen, Jack and 10 all of the same suit. b. Straight flush: Five cards of sequential rank, all of the same suit. c. Four of a kind: Four cards of the same rank and one other card. d. Full house: Three matching cards of one rank and two matching cards of another rank. e. Flush: Any five cards of the same suit, but not in a sequence. f. Straight: Five cards of sequential rank in at least two different suits. g. Three of a kind: Three cards of the same rank and two other cards. h. Two pairs: Two cards of one rank, two cards of another rank and one other card. i. One pair: Two cards of the same rank and three other cards. j. High card: If no other hand is formed, then the highest card ranking in the hand is used to determine the winner.

  5. Return the best poker hand in the sorted list of tuples based on the above criteria.

Here is the Python code implementation of the above algorithm:

def get_rank_map():
    rank_map = {}
    for i in range(2, 11):
        rank_map[str(i)] = i
    rank_map['A'] = 14
    rank_map['K'] = 13
    rank_map['Q'] = 12
    rank_map['J'] = 11
    return rank_map

def is_royal_flush(cards):
    return cards[0][0] == 'A' and cards[1][0] == 'K' and cards[2][0] == 'Q' and cards[3][0] == 'J' and cards[4][0] == '10' and is_flush(cards)

def is_straight_flush(cards):
    return is_straight(cards) and is_flush(cards)

def is_four_of_a_kind(cards):
    ranks = [card[0] for card in cards]
    return (ranks.count(ranks[0]) == 4 or ranks.count(ranks[-1]) == 4)

def is_full_house(cards):
    ranks = [card[0] for card in cards]
    return (ranks.count(ranks[0]) == 3 and ranks.count(ranks[-1]) == 2) or (ranks.count(ranks[0]) == 2 and ranks.count(ranks[-1]) == 3)

def is_flush(cards):
    suits = [card[1] for card in cards]
    return len(set(suits)) == 1

def is_straight(cards):
    rank_map = get_rank_map()
    ranks = [rank_map[card[0]] for card in cards]
    is_ace_low_straight = sorted(ranks) == [2, 3, 4, 5, 14]
    is_not_ace_low_straight = sorted(ranks) == list(range(min(ranks), max(ranks)+1))
    return is_ace_low_straight or is_not_ace_low_straight

def is_three_of_a_kind(cards):
    ranks = [card[0] for card in cards]
    return ranks.count(ranks[0]) == 3 or ranks.count(ranks[1]) == 3 or ranks.count(ranks[2]) == 3 or ranks.count(ranks[3]) == 3

def is_two_pairs(cards):
    ranks = [card[0] for card in cards]
    return len(set(ranks)) == 3

def is_one_pair(cards):
    ranks = [card[0] for card in cards]
    return len(set(ranks)) == 4

def get_best_poker_hand(cards):
    rank_map = get_rank_map()
    cards.sort(key=lambda x: rank_map[x[0]], reverse=True)
    if is_royal_flush(cards):
        return "Royal flush"
    elif is_straight_flush(cards):
        return "Straight flush"
    elif is_four_of_a_kind(cards):
        return "Four of a kind"
    elif is_full_house(cards):
        return "Full house"
    elif is_flush(cards):
        return "Flush"
    elif is_straight(cards):
        return "Straight"
    elif is_three_of_a_kind(cards):
        return "Three of a kind"
    elif is_two_pairs(cards):
        return "Two pairs"
    elif is_one_pair(cards):
        return "One pair"
    else:
        return "High card"

In this implementation, we have implemented various helper functions to check for different types of poker hands. The main function get_best_poker_hand sorts the list of cards based on their rank and then checks for the presence of different types of poker hands using these helper functions. If a particular poker hand is found, then it returns the name of that poker hand as a string. If no poker hand is found, then it returns "High card" indicating that the highest card in the hand is used to determine the winner.

Best Poker Hand Solution Code

1