Ctrl K

Cheapest String on the Alphabet Wheel

Medium
The letters a to z sit on a wheel, so a comes right after z. Changing one letter into another costs the number of steps between them around the wheel, going the shorter way. For example, c → a costs 2, and so does y → a (via z). You get a string text and a budget budget. You may change any letters of text as long as the total cost of all changes is at most budget. Return the lexicographically smallest string you can end up with.
Example 1
text="ybcz", budget=4 → 'aabz'

y → a costs 2 steps (via z), b → a costs 1 more. One step is left, so c becomes b. Nothing remains for z.

Example 2
text="hello", budget=20 → 'aaclo'

h → a costs 7 and e → a costs 4, leaving 9. Turning l into a would take 11, so l moves down by 9 and becomes c.

Example 3
text="mno", budget=0 → 'mno'

With a zero budget the string stays as it is.

Constraints
  • 1 ≤ len(text) ≤ 100
  • 0 ≤ budget ≤ 2000
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumEvery Letter Once MediumErase Digits for the Smallest Number MediumSmallest Up-Down Sequence MediumRing of Flippable Words Medium
loading Python…
Input
text="ybcz", budget=4
Expected
'aabz'

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