3472. Longest Palindromic Subsequence After at Most K Operations
SourceWeekly Contest 439 Q2DifficultyMediumRating1883
Description
You are given a string s and an integer k.
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'.
Return the length of the longest palindromic subsequence of s that can be obtained after performing at most k operations.
Example 1:
Input: s = "abced", k = 2
Output: 3
Explanation:
- Replace
s[1]with the next letter, andsbecomes"acced". - Replace
s[4]with the previous letter, andsbecomes"accec".
The subsequence "ccc" forms a palindrome of length 3, which is the maximum.
Example 2:
Input: s = "aaazzz", k = 4
Output: 6
Explanation:
- Replace
s[0]with the previous letter, andsbecomes"zaazzz". - Replace
s[4]with the next letter, andsbecomes"zaazaz". - Replace
s[3]with the next letter, andsbecomes"zaaaaz".
The entire string forms a palindrome of length 6.
Constraints:
1 <= s.length <= 2001 <= k <= 200sconsists of only lowercase English letters.
Solutions
Solution 1: Memoized Search
Thinking
An operation moves a letter to an adjacent letter on the cycle, at most \(k\) times; we want the longest palindromic subsequence. Pairing the two ends costs the circular alphabet distance.
Classic LPS plus a remaining-budget dimension. State \((i,j,k)\) has size \(n^2(k+1)\) and memoizes well.
Transitions drop the left or right character, or spend \(t=\min(|s_i-s_j|,26-|s_i-s_j|)\) to pair them. Empty and singleton intervals are the base.
We design a function \(\textit{dfs}(i, j, k)\), which represents the length of the longest palindromic subsequence that can be obtained in the substring \(s[i..j]\) with at most \(k\) operations. The answer is \(\textit{dfs}(0, n - 1, k)\).
The calculation process of the function \(\textit{dfs}(i, j, k)\) is as follows:
- If \(i > j\), return \(0\);
- If \(i = j\), return \(1\);
- Otherwise, we can ignore \(s[i]\) or \(s[j]\) and calculate \(\textit{dfs}(i + 1, j, k)\) and \(\textit{dfs}(i, j - 1, k)\) respectively; or we can change \(s[i]\) and \(s[j]\) to the same character and calculate \(\textit{dfs}(i + 1, j - 1, k - t) + 2\), where \(t\) is the ASCII code difference between \(s[i]\) and \(s[j]\).
- Return the maximum value of the above three cases.
To avoid repeated calculations, we use memoized search.
The time complexity is \(O(n^2 \times k)\), and the space complexity is \(O(n^2 \times k)\). Where \(n\) is the length of the string \(s\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |