Best Poker Hand - Problem
Poker Hand Evaluator

You're given two arrays representing a 5-card poker hand:

โ€ข ranks: An integer array where ranks[i] represents the rank of the i-th card
โ€ข suits: A character array where suits[i] represents the suit of the i-th card

Your task is to determine the best possible poker hand you can make from these 5 cards. The poker hands are ranked from best to worst as follows:

๐Ÿ† "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 (when no other pattern exists)

Goal: Return a string representing the best poker hand type you can form.

Note: The return values are case-sensitive and must match exactly.

Input & Output

example_1.py โ€” Basic Three of a Kind
$ Input: ranks = [13,2,13,13,3], suits = ["c","d","c","h","d"]
โ€บ Output: "Three of a Kind"
๐Ÿ’ก Note: We have three cards with rank 13 (three Kings), which forms a Three of a Kind - the second-best possible hand type.
example_2.py โ€” Flush Hand
$ Input: ranks = [4,4,2,4,4], suits = ["d","d","d","d","d"]
โ€บ Output: "Flush"
๐Ÿ’ก Note: All five cards have the same suit 'd' (diamonds), making this a Flush - the best possible hand type, even though we also have a four of a kind in ranks.
example_3.py โ€” Simple Pair
$ Input: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
โ€บ Output: "Pair"
๐Ÿ’ก Note: We have two cards with rank 10, forming a Pair. The suits are all different, so no flush is possible.

Visualization

Tap to expand
Poker Hand Evaluation: Hash Table ApproachStep 1: Input AnalysisCards: [7โ™ , 7โ™ฅ, Kโ™ฆ, 7โ™ฃ, 2โ™ ] โ†’ Count suits and ranks simultaneouslySuit Count: {โ™ :2, โ™ฅ:1, โ™ฆ:1, โ™ฃ:1} | Rank Count: {7:3, K:1, 2:1}Step 2: Pattern Recognitionโœ— Flush Check:len(suit_count) = 4 โ‰  1No flush possibleโœ“ Three of a Kind:max(rank_count) = 3 โ‰ฅ 3Step 3: ResultBest Hand Found:Three of a KindThree 7s beats any pair or high cardAlgorithm Efficiencyโšก Time: O(n) - Single pass through cards๐Ÿ’พ Space: O(n) - Hash maps for counting๐ŸŽฏ Key Insight: Frequency counting reveals all patterns efficiently
Understanding the Visualization
1
Count Frequencies
Scan through all 5 cards once, counting how many of each suit and rank we have
2
Check Flush
If we have only 1 unique suit, all cards match - that's a Flush!
3
Check Three of a Kind
If any rank appears 3+ times, we have Three of a Kind
4
Check Pair
If any rank appears 2+ times, we have at least a Pair
Key Takeaway
๐ŸŽฏ Key Insight: Instead of comparing every card with every other card, we count frequencies once and analyze the counts - much more efficient and easier to extend for additional hand types!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all n cards to count frequencies

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash maps store at most n different suits and ranks

n
2n
โšก Linearithmic Space

Constraints

  • ranks.length == suits.length == 5
  • 1 โ‰ค ranks[i] โ‰ค 13
  • suits[i] is one of 'a', 'b', 'c', 'd'
  • Return values are case-sensitive
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
28.7K Views
Medium Frequency
~12 min Avg. Time
845 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