Interview Questions/Coding/Shard Partition Lengths

Shard Partition Lengths

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Shard Partition Lengths

A log string must be split into contiguous shards. A valid split requires that every character appears in at most one shard. Once a character appears in one shard, all of its occurrences must be contained inside that same shard. The platform wants as many shards as possible while preserving the original order of the log. Return the length of each shard in order. Input Format: You are given a string s. Output Format: Return an array containing the lengths of the maximum number of valid shards.

Examples

Example 1
Input:
s = "ababcbacadefegdehijhklij"
Output:
9 7 8

Explanation: The partitions are ["ababcbaca", "defegde", "hijhklij"] with lengths [9, 7, 8]. Hence the output is 9 7 8.

Approach hint

Know the last occurrence of each code.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp