2347. Best Poker Hand LeetCode Solution

In this guide, you will get 2347. Best Poker Hand LeetCode Solution with the best time and space complexity. The solution to Best Poker Hand problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Best Poker Hand solution in C++
  4. Best Poker Hand solution in Java
  5. Best Poker Hand solution in Python
  6. Additional Resources
2347. Best Poker Hand LeetCode Solution image

Problem Statement of 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:

“Flush”: Five cards of the same suit.
“Three of a Kind”: Three cards of the same rank.
“Pair”: Two cards of the same rank.
“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.

Complexity Analysis

  • Time Complexity: O(|\texttt{ranks}| + |\texttt{suits}|)
  • Space Complexity: O(1)

2347. Best Poker Hand LeetCode Solution in C++

class Solution {
 public:
  string bestHand(vector<int>& ranks, vector<char>& suits) {
    if (ranges::all_of(suits,
                       [&suits](const char suit) { return suit == suits[0]; }))
      return "Flush";

    constexpr int kMax = 13;
    vector<int> count(kMax + 1);

    for (const int rank : ranks)
      ++count[rank];

    const int mx = ranges::max(count);
    if (mx > 2)
      return "Three of a Kind";
    if (mx == 2)
      return "Pair";
    return "High Card";
  }
};
/* code provided by PROGIEZ */

2347. Best Poker Hand LeetCode Solution in Java

class Solution {
  public String bestHand(int[] ranks, char[] suits) {
    if (new String(suits).chars().allMatch(s -> s == suits[0]))
      return "Flush";

    final int kMax = 13;
    int[] count = new int[kMax + 1];

    for (final int rank : ranks)
      ++count[rank];

    final int mx = Arrays.stream(count).max().getAsInt();
    if (mx > 2)
      return "Three of a Kind";
    if (mx == 2)
      return "Pair";
    return "High Card";
  }
}
// code provided by PROGIEZ

2347. Best Poker Hand LeetCode Solution in Python

class Solution:
  def bestHand(self, ranks: list[int], suits: list[str]) -> str:
    if all(suit == suits[0] for suit in suits):
      return 'Flush'

    match max(Counter(ranks).values()):
      case 5 | 4 | 3:
        return 'Three of a Kind'
      case 2:
        return 'Pair'
      case _:
        return 'High Card'
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.