3472. Longest Palindromic Subsequence After at Most K Operations Medium
1/**
2 * [3472] Longest Palindromic Subsequence After at Most K Operations
3 *
4 * You are given a string s and an integer k.
5 * In one operation, you can replace the character at any position with the next or previous letter in the alphabet (wrapping around so that 'a' is after 'z'). For example, replacing 'a' with the next letter results in 'b', and replacing 'a' with the previous letter results in 'z'. Similarly, replacing 'z' with the next letter results in 'a', and replacing 'z' with the previous letter results in 'y'.
6 * Return the length of the longest <span data-keyword="palindrome-string">palindromic</span> <span data-keyword="subsequence-string-nonempty">subsequence</span> of s that can be obtained after performing at most k operations.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "abced", k = 2</span>
11 * Output: <span class="example-io">3</span>
12 * Explanation:
13 *
14 * Replace s[1] with the next letter, and s becomes "acced".
15 * Replace s[4] with the previous letter, and s becomes "accec".
16 *
17 * The subsequence "ccc" forms a palindrome of length 3, which is the maximum.
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">s = "</span>aaazzz<span class="example-io">", k = 4</span>
22 * Output: 6
23 * Explanation:
24 *
25 * Replace s[0] with the previous letter, and s becomes "zaazzz".
26 * Replace s[4] with the next letter, and s becomes "zaazaz".
27 * Replace s[3] with the next letter, and s becomes "zaaaaz".
28 *
29 * The entire string forms a palindrome of length 6.
30 * </div>
31 *
32 * Constraints:
33 *
34 * 1 <= s.length <= 200
35 * 1 <= k <= 200
36 * s consists of only lowercase English letters.
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/longest-palindromic-subsequence-after-at-most-k-operations/
42// discuss: https://leetcode.com/problems/longest-palindromic-subsequence-after-at-most-k-operations/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn longest_palindromic_subsequence(s: String, k: i32) -> i32 {
48 0
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_3472() {
60 }
61}
62Back
© 2026 bowen.ge All Rights Reserved.