Longest Palindromic Subsequence After at Most K Operations

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'.
  • Replacing 'a' with the previous letter results in 'z'.
  • Replacing 'z' with the next letter results in 'a'.
  • 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
Inputs = "abced", k = 2
Output3
After changing s to "accec", the subsequence "ccc" forms a palindrome of length 3, which is the maximum.
Example 2
Inputs = "aaazzz", k = 4
Output6
After the operations, the entire string can form a palindrome of length 6.

Constraints

  • 1 <= s.length <= 200
  • 1 <= k <= 200
  • s consists of only lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate