All problems
0966HardArrayDynamic ProgrammingBit ManipulationBitmaskBipartite GraphMatching (Graph)Perfect Matching

Handing Out Badges Nobody Shares

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1434Number of Ways to Wear Different Hats to Each Other

This 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.

Examples

Example 1

Input
hats = [[1, 2], [1, 2]]
Output
2

Both staff would take either badge, so one takes 1 and the other 2, or the other way round.

Example 2

Input
hats = [[1], [1]]
Output
0

Both staff would only take badge 1, and it cannot be shared.

Example 3

Input
hats = [[1, 2, 3, 4], [2, 3], [3]]
Output
2

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.

Constraints

  • 1 <= hats.length <= 10
  • 1 <= hats[i].length <= 40
  • 1 <= hats[i][j] <= 40
  • The numbers in hats[i] are all different

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def number_ways(hats: list[list[int]]) -> int:
Java
public int numberWays(List<List<Integer>> hats)
September 7
Apply