Ctrl K

Two-Key Typewriter

Medium
An old typewriter has only two working keys left: - **Add** appends the letter a to the end of the text; - **Shift** replaces the last letter of the text with the next letter of the alphabet (a → b, …, y → z, and z wraps back to a). The text starts out empty, so the first press can only be Add. You need to type the word word using as few key presses as possible. Return the list of every text that appears after each press, in the order they appear.
Example 1
word="bad" → ['a', 'b', 'ba', 'baa', 'bab', 'bac', 'bad']

The first letter b: the screen shows a, then b. Next we add a, which is already the needed letter: ba. The third letter: baa, bab, bac, bad.

Example 2
word="ca" → ['a', 'b', 'c', 'ca']

Getting c takes three states: a, b, c. The second letter a appears right after it is added.

Constraints
  • 1 ≤ len(word) ≤ 400
  • word consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMultiplying Digit Strings MediumMultiply Complex Numbers in Strings MediumSeeded Playoff Bracket MediumTotal of a Fraction Chain MediumSolve for x Medium
loading Python…
Input
word="bad"
Expected
['a', 'b', 'ba', 'baa', 'bab', 'bac', 'bad']

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