ExamAdda Logo

Partition Labels

Medium

You are given a string s.

You want to partition the string into as many parts as possible so that:

  • Each character appears in at most one part.
  • The parts must maintain their original order.
  • When all parts are concatenated, they must form the original string.

Return a list containing the size of each partition.

Example 1

Input

s = "ababcbacadefegdehijhklij"

Output

[9, 7, 8]

Explanation

The string can be partitioned as:

ababcbaca
defegde
hijhklij

Their lengths are:

9 7 8

 

Each character occurs in only one partition.

Example 2

Input

s = "eccbbbbdec"

Output

[10]

Explanation

The characters e, c, and b occur throughout the string, so the entire string must belong to one partition.

Constraints

1 <= s.length <= 500
s consists of lowercase English letters.
The string can be divided into one or more partitions.

Hints:

Hint 1

Find the last occurrence of every character in the string.

Hint 2

Start a partition from the current position and keep extending it until the current partition reaches the last occurrence of every character inside it.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 26, 2026

Last updated

Aug 26, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output