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

# Longest Ideal Subsequence Python Solution

> Tested Python solution for LeetCode 2370 with 27 pytest cases. Generate a practice environment with lcpy.

LeetCode 2370, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/longest-ideal-subsequence/description/).

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

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

## Problem

You are given a string `s` consisting of lowercase letters and an integer `k`. We call a string `t` **ideal** if the following conditions are satisfied:

* `t` is a **subsequence** of the string `s`.
* The absolute difference in the alphabet order of every two **adjacent** letters in `t` is less than or equal to `k`.

Return *the length of the **longest** ideal string*.

A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

**Note** that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of `'a'` and `'z'` is `25`, not `1`.

### Examples

```
Input: s = "acfgbd", k = 2
Output: 4
```

**Explanation:** The longest ideal string is `"acbd"`. The length of this string is `4`, so `4` is returned.
Note that `"acfgbd"` is not ideal because `'c'` and `'f'` have a difference of `3` in alphabet order.

```
Input: s = "abcd", k = 3
Output: 4
```

**Explanation:** The longest ideal string is `"abcd"`. The length of this string is `4`, so `4` is returned.

### Constraints

* `1 <= s.length <= 10^5`
* `0 <= k <= 25`
* `s` consists of lowercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(26 * n)
    # Space: O(26)
    def longest_ideal_string(self, s: str, k: int) -> int:
        best = [0] * 26
        for ch in s:
            c = ord(ch) - ord("a")
            window = best[max(0, c - k) : min(26, c + k + 1)]
            best[c] = max(best[c], 1 + max(window))
        return max(best)
```

## Complexity

| Time | Space |
| - | - |
| O(26 \* n) | O(26) |

## Tags

[NeetCode All](/catalog/neetcode).


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