Best Poker Hand - Problem
Poker Hand Evaluator
You're given two arrays representing a 5-card poker hand:
โข
โข
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.
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 cardYour 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
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
โ Linear Growth
Space Complexity
O(n)
Hash maps store at most n different suits and ranks
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code