Practice/Microsoft/Leetcode 2370. Longest Ideal Subsequence
CodingOptional
You are given a string s consisting of lowercase English letters and a non-negative integer k.
A valid chain is a subsequence of s where for every pair of consecutive characters in the subsequence, the absolute difference between their positions in the alphabet is at most k.
For example, if k = 2, the character 'c' can be followed by any character from 'a' to 'e' (positions differ by at most 2).
Your task is to find the length of the longest valid chain that can be formed from the string.
ks consists only of lowercase English lettersExample 1:
` Input: s = "acfgbd", k = 2 Output: 4 Explanation: One valid chain is "acbd":
Example 2:
Input: s = "abcd", k = 3 Output: 4 Explanation: All characters form a valid chain since consecutive letters differ by 1.
Example 3:
Input: s = "xyz", k = 0 Output: 1 Explanation: With k=0, only identical consecutive characters are allowed, so best chain length is 1.