All problems
0230MediumHash TableTwo PointersStringGreedy

Duty Roster Blocks

Tracked in this browser only
Write code

Trains the technique from

LeetCode 763Partition Labels

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.

A workshop posts its watch roster as roster, a string of lowercase letters. The letter in each slot is the code of the technician standing that watch, and the slots run left to right in the order they will be worked.

The supervisor wants to hand the roster out in consecutive blocks. Every slot belongs to exactly one block and the blocks stay in order, so reading the blocks one after another spells roster again. A block may only be handed out if no technician's code turns up in any other block. Split the roster into as many blocks as that allows, which fixes one answer.

Return the number of slots in each block, in the order the blocks are worked.

Examples

Example 1

Input
roster = "zzxyxwwv"
Output
[2, 3, 2, 1]

The blocks are `zz`, `xyx`, `ww` and `v`, so their slot counts are 2, 3, 2 and 1. Codes z, x, y, w and v each sit inside one block only.

Example 2

Input
roster = "aabbaacc"
Output
[6, 2]

The blocks are `aabbaa` and `cc`. Codes a and b are confined to the first block and code c to the second.

Example 3

Input
roster = "wxyz"
Output
[1, 1, 1, 1]

No code repeats anywhere in the roster, and the four one-slot blocks each hold a different code.

Constraints

  • 1 <= roster.length <= 500
  • roster holds lowercase English letters only

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 duty_roster_blocks(roster: str) -> list[int]:
Java
public List<Integer> dutyRosterBlocks(String roster)
September 7
Apply