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

# Unique Substrings in Wraparound String

> Tested Python solution for LeetCode 467 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 467, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/).

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

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

## Problem

We define the string `base` to be the infinite wraparound string of `"abcdefghijklmnopqrstuvwxyz"`, so `base` will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".

Given a string `s`, return the number of **unique non-empty substrings** of `s` are present in `base`.

### Examples

```
Input: s = "a"
Output: 1
```

**Explanation:** Only the substring "a" of s is in base.

```
Input: s = "cac"
Output: 2
```

**Explanation:** There are two substrings ("a", "c") of s in base.

```
Input: s = "zab"
Output: 6
```

**Explanation:** There are six substrings ("z", "a", "b", "za", "ab", and "zab") of s in base.

### Constraints

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

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def find_substring_in_wrapround_string(self, s: str) -> int:
        best: dict[str, int] = {}
        run = 0
        for i, ch in enumerate(s):
            if i > 0 and (ord(ch) - ord(s[i - 1])) % 26 == 1:
                run += 1
            else:
                run = 1
            best[ch] = max(best.get(ch, 0), run)
        return sum(best.values())
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


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