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.text="abcxa", turns=1 → 4
Turn c one step back to b to get abbxa, which contains the palindromic subsequence abba of length 4.
text="azza", turns=0 → 4
No turns are allowed, but the whole string is already a palindrome.
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.
text) ≤ 200turns ≤ 200text consists of lowercase English letterstext="abcxa", turns=1
4
Run checks the first 3 tests in your browser. Submit checks all 153 tests on the server, hidden ones included.