Class Solution
-
- All Implemented Interfaces:
public final class Solution3472 - Longest Palindromic Subsequence After at Most K Operations.
Medium
You are given a string
sand an integerk.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
sthat can be obtained after performing at mostkoperations.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.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerlongestPalindromicSubsequence(String s, Integer k)-
-
Method Detail
-
longestPalindromicSubsequence
final Integer longestPalindromicSubsequence(String s, Integer k)
-
-
-
-