> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Hand of Straights Python Solution with Tests

> Tested Python solution for LeetCode 846 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 846, Medium. Topics: Array, Hash Table, Greedy, Sorting. [View on LeetCode](https://leetcode.com/problems/hand-of-straights/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 846   # by problem number
lcpy gen -s hand_of_straights   # by problem name
```

## Problem

Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size `groupSize`, and consists of `groupSize` consecutive cards.

Given an integer array `hand` where `hand[i]` is the value written on the `i<sup>th</sup>` card and an integer `groupSize`, return `true` if she can rearrange the cards, or `false` otherwise.

### Examples

```
Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
```

```
Input: hand = [1,2,3,4,5], groupSize = 4
Output: false
Explanation: Alice's hand can not be rearranged into groups of 4.
```

### Constraints

* 1 \<= hand.length \<= 10\<sup>4\</sup>
* 0 \<= hand\[i] \<= 10\<sup>9\</sup>
* 1 \<= groupSize \<= hand.length

**Note:** This question is the same as 1296: [https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/hand_of_straights/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/hand_of_straights/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def is_n_straight_hand(self, hand: list[int], group_size: int) -> bool:
        if len(hand) % group_size != 0:
            return False

        count: Counter[int] = Counter(hand)
        for card in sorted(count):
            if count[card] == 0:
                continue
            frequency = count[card]
            # Greedily form `frequency` groups starting at `card`
            for offset in range(group_size):
                next_card = card + offset
                if count[next_card] < frequency:
                    return False
                count[next_card] -= frequency
        return True
```

## Complexity

| Time       | Space |
| ---------- | ----- |
| O(n log n) | O(n)  |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
