Ctrl K

Palindrome on a Letter Dial

Medium
The string text is spelled on a dial of the 26 lowercase English letters arranged in a circle: a is followed by b, b by c, …, and z wraps back to a. One turn changes any single letter of the string to its neighbor on the dial, either the next or the previous one. For example, a can become b or z, and z can become a or y. You may make at most turns turns in total, and the same letter may be turned more than once. After turning, pick a subsequence of the string (some of its letters in their original order, not necessarily adjacent) that reads the same forwards and backwards. Return the greatest possible length of such a subsequence.
Example 1
text="abcxa", turns=1 → 4

Turn c one step back to b to get abbxa, which contains the palindromic subsequence abba of length 4.

Example 2
text="azza", turns=0 → 4

No turns are allowed, but the whole string is already a palindrome.

Example 3
text="mz", turns=5 → 1

m and z are 13 steps apart either way around the dial, which is more than the budget, so the best is a single letter.

Constraints
  • 1 ≤ len(text) ≤ 200
  • 0 ≤ turns ≤ 200
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Mirror Fragment MediumAll Balanced Bracket Strings MediumFewest Letter Edits MediumWays to Read a Digit Code MediumWeaving Two Strings Medium
loading Python…
Input
text="abcxa", turns=1
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 153 tests on the server, hidden ones included.