> ## 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.

# Split a String Into the Max Number of Unique

> Tested Python solution for LeetCode 1593 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1593, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/split-a-string-into-the-max-number-of-unique-substrings/description/).

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

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

## Problem

Given a string `s`, return the maximum number of unique substrings that the given string can be split into.

You can split string `s` into any list of **non-empty substrings**, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are **unique**.

A **substring** is a contiguous sequence of characters within a string.

### Examples

```
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
```

```
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
```

```
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
```

### Constraints

* `1 <= s.length <= 16`
* `s` contains only lower case English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * 2^n) worst case, pruned hard by the remaining-suffix bound
    # Space: O(n) recursion depth plus the seen-set of at most n pieces
    def max_unique_split(self, s: str) -> int:
        n = len(s)
        seen: set[str] = set()

        def dfs(start: int, count: int) -> int:
            best = count
            for end in range(start + 1, n + 1):
                piece = s[start:end]
                # Even taking every remaining character as its own split cannot
                # beat the best found so far.
                if piece in seen or count + 1 + (n - end) <= best:
                    continue
                seen.add(piece)
                best = max(best, dfs(end, count + 1))
                seen.remove(piece)
            return best

        return dfs(0, 0)
```

## Complexity

| Time | Space |
| - | - |
| O(n \* 2^n) worst case, pruned hard by the remaining-suffix bound | O(n) recursion depth plus the seen-set of at most n pieces |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.