> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Partition Labels Python Solution with Tests

> Tested Python solution for LeetCode 763 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 763, Medium. Topics: Hash Table, Two Pointers, String, Greedy. [View on LeetCode](https://leetcode.com/problems/partition-labels/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 763   # by problem number
lcpy gen -s partition_labels   # by problem name
```

## Problem

You are given a string `s`. We want to partition the string into as many parts as possible so that each letter appears in at most one part. For example, the string `"ababcc"` can be partitioned into `["abab", "cc"]`, but partitions such as `["aba", "bcc"]` or `["ab", "ab", "cc"]` are invalid.

Note that the partition is done so that after concatenating all the parts in order, the resultant string should be `s`.

Return *a list of integers representing the size of these parts*.

### Examples

```
Input: s = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation:
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
```

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/partition_labels/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/partition_labels/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) — alphabet bounded to 26
    def partition_labels(self, s: str) -> list[int]:
        last_occurrence: dict[str, int] = {char: idx for idx, char in enumerate(s)}
        partitions: list[int] = []
        start, end = 0, 0
        for idx, char in enumerate(s):
            end = max(end, last_occurrence[char])
            if idx == end:
                partitions.append(idx - start + 1)
                start = idx + 1
        return partitions
```

## Complexity

| Time | Space                         |
| ---- | ----------------------------- |
| O(n) | O(1) — alphabet bounded to 26 |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
