{x}
blog image

Best Poker Hand

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.

Solution Explanation: Best Poker Hand

This problem asks to determine the best possible poker hand from a given set of 5 cards represented by their ranks and suits. The solution involves checking for different hand types in a specific order (Flush, Three of a Kind, Pair, High Card), returning the best hand found.

Approach:

The solution employs a straightforward approach using counting and a boolean flag for checking the flush condition.

  1. Flush Check: First, it checks if all cards share the same suit. This is done efficiently by comparing each suit to the first suit. If all are the same, it's a "Flush," and the function returns immediately.

  2. Rank Counting: If it's not a flush, a frequency count of the ranks is created using a hash table (or an array in the code examples) to store the count of occurrences for each rank.

  3. Hand Type Determination: The algorithm then iterates through the rank counts:

    • If any rank has a count of 3 or more, it's a "Three of a Kind".
    • Otherwise, if any rank has a count of 2, it's a "Pair".
    • If neither of the above conditions is met, it's a "High Card".

Time Complexity Analysis:

  • The flush check iterates through the suits array once, taking O(n) time, where n is the number of cards (5 in this case).
  • The rank counting iterates through the ranks array once, taking O(n) time.
  • The hand type determination iterates through the rank counts, which is also O(n) in the worst case (all ranks are distinct).

Therefore, the overall time complexity is O(n), which is linear with respect to the number of cards. In this specific problem, it's effectively O(1) because n is always 5.

Space Complexity Analysis:

  • The space used is dominated by the rank count array/hash table. In the worst case, this array needs to store counts for all possible ranks (13 ranks). Thus, the space complexity is O(1) (constant space) because the size of the rank count array is fixed and independent of the input size. In the examples, arrays of size 14 are used to accommodate ranks from 1 to 13 inclusive. This is also a constant.

Code Examples and Explanation (Python):

from collections import Counter
from itertools import pairwise
 
class Solution:
    def bestHand(self, ranks: List[int], suits: List[str]) -> str:
        # Efficient Flush Check: using pairwise from itertools for cleaner code
        if all(a == b for a, b in pairwise(suits)):  
            return "Flush"
        
        # Rank Counting using Counter
        rank_counts = Counter(ranks)
        
        # Hand Type Determination
        if any(count >= 3 for count in rank_counts.values()):
            return "Three of a Kind"
        elif any(count == 2 for count in rank_counts.values()):
            return "Pair"
        else:
            return "High Card"
 

The other language examples follow a very similar structure, using arrays or hash maps to count rank frequencies and applying the same logic for determining the best hand type. The key is the efficient flush check and the concise use of data structures for rank counting.