Leetcode 2347: Best Poker Hand

grid47
grid47
Exploring patterns and algorithms
Mar 17, 2024 6 min read

You are given an integer array ranks and a character array suits. These represent a set of 5 playing cards, where each card has a rank (from 1 to 13) and a suit (one of ‘a’, ‘b’, ‘c’, or ’d’). Determine the best possible poker hand you can make from the cards.
Problem
Approach
Steps
Complexity
Input: The input consists of two arrays, `ranks` and `suits`, both of length 5. `ranks[i]` is the rank of the ith card and `suits[i]` is the suit of the ith card.
Example: ranks = [8, 3, 5, 12, 8], suits = ["a", "b", "a", "a", "a"]
Constraints:
• ranks.length == 5
• suits.length == 5
• 1 <= ranks[i] <= 13
• 'a' <= suits[i] <= 'd'
Output: The output should be a string representing the best type of poker hand that can be made from the given cards.
Example: "Flush"
Constraints:
• The output must be one of the following: 'Flush', 'Three of a Kind', 'Pair', or 'High Card'.
Goal: To determine the best hand from the set of 5 cards.
Steps:
• Check if all cards have the same suit for a flush.
• Check if there are three cards with the same rank for a 'Three of a Kind'.
• Check if there are two cards with the same rank for a 'Pair'.
• If none of the above, return 'High Card'.
Goal: The input follows the given constraints.
Steps:
• 1 <= ranks[i] <= 13
• 'a' <= suits[i] <= 'd'
• No two cards have the same rank and suit.
Assumptions:
• The input always contains exactly 5 cards.
• Ranks and suits are valid according to the constraints.
Input: ranks = [8, 3, 5, 12, 8], suits = ["a", "b", "a", "a", "a"]
Explanation: All cards have the same suit 'a', so we have a Flush.

Input: ranks = [11, 11, 9, 11, 5], suits = ["b", "b", "c", "d", "a"]
Explanation: Three cards have the same rank (11), so we have a 'Three of a Kind'.

Input: ranks = [7, 2, 7, 9, 6], suits = ["d", "d", "b", "a", "b"]
Explanation: Two cards have the same rank (7), so we have a 'Pair'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus