Difficulty: Easy | Acceptance: 61.80% | Paid: No Topics: Array, Hash Table, Counting
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.
- Examples
- Constraints
- Hash Map Counting
- Array-based Counting
Examples
Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
Output: "Flush"
Explanation: The hand with all cards of the same suit is a flush.
Input: ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
Output: "Three of a Kind"
Explanation: The hand with three cards of the same rank is a three of a kind.
Input: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
Output: "Pair"
Explanation: The hand with two cards of the same rank is a pair.
Constraints
ranks.length == suits.length == 5
1 <= ranks[i] <= 13
'a' <= suits[i] <= 'd'
Hash Map Counting
Intuition Use a hash map to count the frequency of each rank and check if all suits are identical. Evaluate hands in order of priority: Flush first, then Three of a Kind, Pair, and finally High Card.
Steps
- Check if all suits are the same (Flush)
- Count the frequency of each rank using a hash map
- Check if any rank appears 3 or more times (Three of a Kind)
- Check if any rank appears 2 or more times (Pair)
- Return “High Card” if none of the above conditions are met
from collections import Counter
from typing import List
class Solution:
def bestHand(self, ranks: List[int], suits: List[str]) -> str:
# Check for Flush
if len(set(suits)) == 1:
return \"Flush\"
# Count ranks
rank_count = Counter(ranks)
# Check for Three of a Kind
if any(count >= 3 for count in rank_count.values()):
return \"Three of a Kind\"
# Check for Pair
if any(count >= 2 for count in rank_count.values()):
return \"Pair\"
return \"High Card\"Complexity
- Time: O(n) where n is the number of cards (always 5)
- Space: O(n) for storing the rank counts
- Notes: Hash map provides O(1) average time for insert and lookup operations
Array-based Counting
Intuition Since ranks are limited to values 1-13, we can use a fixed-size array instead of a hash map for counting. This eliminates hash function overhead and provides direct index access.
Steps
- Check if all suits are the same (Flush)
- Create an array of size 14 to count rank frequencies
- Find the maximum frequency among all ranks
- Return the appropriate hand type based on the maximum frequency
from typing import List
class Solution:
def bestHand(self, ranks: List[int], suits: List[str]) -> str:
# Check for Flush
if len(set(suits)) == 1:
return \"Flush\"
# Count ranks using array
rank_count = [0] * 14
for rank in ranks:
rank_count[rank] += 1
max_count = max(rank_count)
if max_count >= 3:
return \"Three of a Kind\"
elif max_count >= 2:
return \"Pair\"
else:
return \"High Card\"Complexity
- Time: O(n) where n is the number of cards (always 5)
- Space: O(1) since the array size is fixed at 14 regardless of input
- Notes: More memory efficient than hash map approach with better cache locality