Split a String into the Most Letter-Disjoint Pieces

Quick Overview

Split a String into the Most Letter-Disjoint Pieces. Cut s into as many consecutive nonempty parts as possible so that no letter occurs in two different parts.

Split a String into the Most Letter-Disjoint Pieces

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Cut s into as many consecutive nonempty parts as possible so that no letter occurs in two different parts. Concatenating the parts in order must recover s. Return their lengths in order. ```cpp class Solution { public: vector<int> partitionLabels(string s); }; ``` ### Examples **Example 1:** ```text s = "abacdeed" Output: [3,1,4] Explanation: The maximal split is "aba", "c", "deed". ``` **Example 2:** ```text s = "xyz" Output: [1,1,1] Explanation: Each distinct letter can form its own part. ``` ### Constraints - String length lies in [1,500]. - All characters are lowercase English letters. ### Contract considerations Preserve order and use each original character once. ```hint Read the boundary condition What is the earliest end allowed by every letter already included? ``` ### Boundary reminders Preserve order and use each original character once.

Overview: Split a String into the Most Letter-Disjoint Pieces. Cut s into as many consecutive nonempty parts as possible so that no letter occurs in two different parts.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Cut s into as many consecutive nonempty parts as possible so that no letter occurs in two different parts. Concatenating the parts in order must recover s. Return their lengths in order.

class Solution {
public:
    vector<int> partitionLabels(string s);
};

Examples

Example 1:

s = "abacdeed"
Output: [3,1,4]
Explanation: The maximal split is "aba", "c", "deed".

Example 2:

s = "xyz"
Output: [1,1,1]
Explanation: Each distinct letter can form its own part.

Constraints

  • String length lies in [1,500].
  • All characters are lowercase English letters.

Contract considerations

Preserve order and use each original character once.

Boundary reminders

Preserve order and use each original character once.