Skip to content

Practice Longest Ideal Subsequence

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2370
LeetCode Problem

Longest Ideal Subsequence

Problem Statement

Choose a subsequence of lowercase string s in which adjacent chosen letters differ in alphabet position by at most k. Alphabet distance is not cyclic: a to z is 25. Return the greatest possible subsequence length.

Source: Longest Ideal Subsequence on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

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

Constraints

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

Related LeetCode Problems

Practice more problems similar to Longest Ideal Subsequence.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.