876. Hand of Straights
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks if we can partition a given multiset of cards (represented by an integer array hand) into groups of groupSize consecutive cards. For example, if groupSize is 3, a valid group would be [2, 3, 4].
Solution
Hand of Straights in Python
class Solution:
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
# If the total number of cards is not divisible by groupSize, it's impossible.
if len(hand) % groupSize != 0:
return False
# Use a Counter to store card frequencies.
counts = collections.Counter(hand)
# Get the unique cards and sort them to process in increasing order.
sorted_keys = sorted(counts.keys())
for key in sorted_keys:
# If the count for this card is 0, it means its cards have been used up
# in previous groups. We can skip it.
if counts[key] > 0:
num_groups = counts[key]
# We must form 'num_groups' groups starting with 'key'.
# Check if we have enough cards for the consecutive sequence.
for i in range(groupSize):
card = key + i
# collections.Counter returns 0 for missing keys, so this check is safe.
if counts[card] < num_groups:
return False
# "Use" the cards by decrementing their counts.
counts[card] -= num_groups
return True
Hand of Straights in Java
class Solution {
public boolean isNStraightHand(int[] hand, int groupSize) {
// If the total number of cards is not divisible by groupSize, it's impossible.
if (hand.length % groupSize != 0) {
return false;
}
// Use a TreeMap to store card frequencies. TreeMap keeps keys sorted.
Map<Integer, Integer> counts = new TreeMap<>();
for (int card : hand) {
counts.put(card, counts.getOrDefault(card, 0) + 1);
}
// Iterate through the sorted keys of the map.
for (int card : counts.keySet()) {
int count = counts.get(card);
// If the count for this card is 0, it means its cards have been used up
// in previous groups. We can skip it.
if (count > 0) {
// We must form 'count' groups starting with 'card'.
// Check if we have enough cards for the consecutive sequence.
for (int i = 1; i < groupSize; i++) {
int nextCard = card + i;
// If a required card is not present or not in sufficient quantity, return false.
if (counts.getOrDefault(nextCard, 0) < count) {
return false;
}
// "Use" the cards by decrementing their counts.
counts.put(nextCard, counts.get(nextCard) - count);
}
}
}
return true;
}
}
Hand of Straights in C++
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
// If the total number of cards is not divisible by groupSize, it's impossible.
if (hand.size() % groupSize != 0) {
return false;
}
// Use std::map to store frequencies. It keeps keys sorted, which is crucial for our greedy approach.
map<int, int> counts;
for (int card : hand) {
counts[card]++;
}
// Iterate through the sorted unique cards (keys of the map).
for (auto const& [card, count_val] : counts) {
// If the count for this card is 0, it means its cards have been used up
// in previous groups. We can skip it.
// We must use counts[card] to get the most up-to-date count.
if (counts[card] > 0) {
int num_groups = counts[card];
// We must form 'num_groups' groups starting with 'card'.
// Check if we have enough cards for the consecutive sequence.
for (int i = 0; i < groupSize; ++i) {
int current_card = card + i;
// If a required card is not present or not in sufficient quantity, return false.
if (counts.find(current_card) == counts.end() || counts[current_card] < num_groups) {
return false;
}
// "Use" the cards by decrementing their counts.
counts[current_card] -= num_groups;
}
}
}
return true;
}
};
Hand of Straights in TypeScript
function isNStraightHand(hand: number[], groupSize: number): boolean {
const n = hand.length;
// If the total number of cards is not divisible by groupSize, it's impossible.
if (n % groupSize !== 0) {
return false;
}
// Use a Map to store card frequencies.
const counts = new Map<number, number>();
for (const card of hand) {
counts.set(card, (counts.get(card) || 0) + 1);
}
// Get the unique cards and sort them to process in increasing order.
const sortedKeys = Array.from(counts.keys()).sort((a, b) => a - b);
for (const key of sortedKeys) {
const numGroups = counts.get(key) || 0;
// If the count for this key is 0, it means its cards have been used up
// in previous groups. We can skip it.
if (numGroups > 0) {
// We must form 'numGroups' groups starting with 'key'.
// Check if we have enough cards for the consecutive sequence.
for (let i = 0; i < groupSize; i++) {
const currentCard = key + i;
const currentCardCount = counts.get(currentCard) || 0;
// If a required card is not present or not in sufficient quantity, return false.
if (currentCardCount < numGroups) {
return false;
}
// "Use" the cards by decrementing their counts.
counts.set(currentCard, currentCardCount - numGroups);
}
}
}
return true;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.