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

# Count Unique Characters of All Substrings of

> Tested Python solution for LeetCode 828 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 828, [Hard](/catalog/hard). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/description/).

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

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

## Problem

\<p>Let's define a function \<code>countUniqueChars(s)\</code> that returns the number of unique characters in \<code>s\</code>.\</p>

\<ul>
\<li>For example, calling \<code>countUniqueChars(s)\</code> if \<code>s = "LEETCODE"\</code> then \<code>"L"\</code>, \<code>"T"\</code>, \<code>"C"\</code>, \<code>"O"\</code>, \<code>"D"\</code> are the unique characters since they appear only once in \<code>s\</code>, therefore \<code>countUniqueChars(s) = 5\</code>.\</li>
\</ul>

\<p>Given a string \<code>s\</code>, return the sum of \<code>countUniqueChars(t)\</code> where \<code>t\</code> is a substring of \<code>s\</code>. The test cases are generated such that the answer fits in a 32-bit integer.\</p>

\<p>Notice that some substrings can be repeated so in this case you have to count the repeated ones too.\</p>

### Examples

```
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Every substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
```

```
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
```

```
Input: s = "LEETCODE"
Output: 92
```

### Constraints

* 1 \<= s.length \<= 10^5
* s consists of uppercase English letters only.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1) (26 alphabet slots)
    def unique_letter_string(self, s: str) -> int:
        # Each character contributes to the substrings in which it is the only
        # occurrence of its letter. For index i with previous occurrence at
        # prev[i] and next occurrence at next[i], the number of such substrings
        # is (i - prev[i]) * (next[i] - i).
        n = len(s)
        prev = [-1] * n
        last: dict[str, int] = {}
        for i, ch in enumerate(s):
            prev[i] = last.get(ch, -1)
            last[ch] = i

        next_pos = [n] * n
        last = {}
        for i in range(n - 1, -1, -1):
            next_pos[i] = last.get(s[i], n)
            last[s[i]] = i

        return sum((i - prev[i]) * (next_pos[i] - i) for i in range(n))
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) (26 alphabet slots) |

## Tags


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