Trains the technique from
LeetCode 1434Number of Ways to Wear Different Hats to Each OtherThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
There are some staff, and hats[i] lists the badge numbers that would suit staff member i. Every list holds distinct numbers.
Every staff member is given exactly one badge that suits them, and no two are given the same badge.
Return how many ways that can be done, modulo 10^9 + 7.
Example 1
Both staff would take either badge, so one takes 1 and the other 2, or the other way round.
Example 2
Both staff would only take badge 1, and it cannot be shared.
Example 3
Badge 3 must go to the third staff member, so badge 2 must go to the second, and the first is left with badge 1 or 4.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def number_ways(hats: list[list[int]]) -> int:public int numberWays(List<List<Integer>> hats)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.