Best Poker Hand - Problem

You are given an integer array ranks and a character array suits. You have 5 cards where the i-th 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.

Input & Output

Example 1 — Three of a Kind
$ Input: ranks = [13,2,10,10,10], suits = ["a","a","b","c","a"]
Output: "Three of a Kind"
💡 Note: Three cards have rank 10, which gives us Three of a Kind (better than checking for flush or pair)
Example 2 — Flush
$ Input: ranks = [4,4,11,9,7], suits = ["d","d","d","d","d"]
Output: "Flush"
💡 Note: All five cards have the same suit 'd', so we have a Flush (best possible hand in this problem)
Example 3 — Pair
$ Input: ranks = [10,10,5,3,1], suits = ["a","b","c","d","e"]
Output: "Pair"
💡 Note: Two cards have rank 10. No flush (all different suits), no three of a kind, so we have a Pair

Constraints

  • ranks.length == suits.length == 5
  • 1 ≤ ranks[i] ≤ 13
  • suits[i] ∈ {'a', 'b', 'c', 'd'}

Visualization

Tap to expand
Best Poker Hand - Optimal Solution INPUT 5 Poker Cards: 13 a 2 a 10 b 10 c 10 a ranks array: [13, 2, 10, 10, 10] suits array: ["a","a","b","c","a"] Hand Types (Best to Worst): 1. Flush (5 same suit) 2. Three of a Kind (3 same rank) 3. Pair (2 same rank) 4. High Card (single card) ALGORITHM STEPS 1 Count Suits Check if all 5 suits match a:3, b:1, c:1 -- Not Flush 2 Count Ranks Build frequency map 13:1, 2:1, 10:3 3 Find Max Count Get highest frequency max_count = 3 (rank 10) 4 Determine Hand Match count to hand type if count == 5: Flush if count >= 3: Three of Kind if count == 2: Pair else: High Card FINAL RESULT Three matching 10s found: 10 b 10 c 10 a Analysis Summary: - Flush? No (3 suits differ) - Three of Kind? Yes (3 tens) - Pair? (skipped - found better) Output: "Three of a Kind" OK - Verified Key Insight: The optimal approach uses hash maps to count occurrences of both suits and ranks in O(n) time. Check for hands in order of strength (Flush first, then Three of Kind, Pair, High Card). Return immediately when best possible hand is found - no need to check weaker hands. TutorialsPoint - Best Poker Hand | Optimal Solution
Asked in
Google 15 Amazon 12
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen