2370. Longest Ideal Subsequence Medium

@problem@discussion
#Hash Table#String#Dynamic Programming



1/**
2 * [2370] Longest Ideal Subsequence
3 *
4 * 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:
5 * 
6 * 	t is a subsequence of the string s.
7 * 	The absolute difference in the alphabet order of every two adjacent letters in t is less than or equal to k.
8 * 
9 * Return the length of the longest ideal string.
10 * 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.
11 * 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.
12 *  
13 * Example 1:
14 * 
15 * Input: s = "acfgbd", k = 2
16 * Output: 4
17 * Explanation: The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
18 * Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.
19 * Example 2:
20 * 
21 * Input: s = "abcd", k = 3
22 * Output: 4
23 * Explanation: The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
24 * 
25 *  
26 * Constraints:
27 * 
28 * 	1 <= s.length <= 10^5
29 * 	0 <= k <= 25
30 * 	s consists of lowercase English letters.
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/longest-ideal-subsequence/
36// discuss: https://leetcode.com/problems/longest-ideal-subsequence/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41    pub fn longest_ideal_string(s: String, k: i32) -> i32 {
42        0
43    }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_2370() {
54    }
55}
56


Back
© 2025 bowen.ge All Rights Reserved.