Split a string into the most parts so each letter appears in only one part

Quick Overview

Split a lowercase string into the largest number of contiguous parts so that every letter appears in only one part, then return the part sizes in order. It tests greedy reasoning about where a cut is safe and a single linear pass over the string.

Split a string into the most parts so each letter appears in only one part

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `s`, partition it into as many parts as possible so that each letter appears in at most one part. Return a list of integers representing the size of these parts. The parts are contiguous pieces of `s`, and joining them in order gives back `s`. ### Function Signature ```python def partition_labels(s: str) -> list[int]: ``` ### Rules - Every part is a non-empty contiguous substring, and every character of `s` belongs to exactly one part. - A letter that appears in one part must not appear in any other part. - Return the part sizes in left-to-right order. Only one partition reaches the maximum number of parts, so the expected output is unique. ### Constraints - `1 <= s.length <= 500` - `s` consists of lowercase English letters. ### Examples ```text Input: s = "ababcbacadefegdehijhklij" Output: [9, 7, 8] ``` The parts are `"ababcbaca"`, `"defegde"` and `"hijhklij"`. No letter appears in two of them, and cutting any of them further would separate two copies of some letter. A split such as `"ababcbacadefegde"`, `"hijhklij"` is also valid, but it has fewer parts. ```text Input: s = "eccbbbbdec" Output: [10] ``` The `e` at index 0 has another copy at index 8, and the `c` at index 1 has another copy at index 9, the last index. The whole string has to stay in one part. ```text Input: s = "caedbdedda" Output: [1, 9] ``` The letter `c` occurs only at index 0, so it forms a part by itself. The `a` at index 1 occurs again at index 9, so everything from index 1 to the end is one part.

Overview: Split a lowercase string into the largest number of contiguous parts so that every letter appears in only one part, then return the part sizes in order. It tests greedy reasoning about where a cut is safe and a single linear pass over the string.

Amazon logo
Amazon
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string s, partition it into as many parts as possible so that each letter appears in at most one part. Return a list of integers representing the size of these parts.

The parts are contiguous pieces of s, and joining them in order gives back s.

Function Signature

def partition_labels(s: str) -> list[int]:

Rules

  • Every part is a non-empty contiguous substring, and every character of s belongs to exactly one part.
  • A letter that appears in one part must not appear in any other part.
  • Return the part sizes in left-to-right order. Only one partition reaches the maximum number of parts, so the expected output is unique.

Constraints

  • 1 <= s.length <= 500
  • s consists of lowercase English letters.

Examples

Input: s = "ababcbacadefegdehijhklij"
Output: [9, 7, 8]

The parts are "ababcbaca", "defegde" and "hijhklij". No letter appears in two of them, and cutting any of them further would separate two copies of some letter. A split such as "ababcbacadefegde", "hijhklij" is also valid, but it has fewer parts.

Input: s = "eccbbbbdec"
Output: [10]

The e at index 0 has another copy at index 8, and the c at index 1 has another copy at index 9, the last index. The whole string has to stay in one part.

Input: s = "caedbdedda"
Output: [1, 9]

The letter c occurs only at index 0, so it forms a part by itself. The a at index 1 occurs again at index 9, so everything from index 1 to the end is one part.