Find the Sequence of Strings Appeared on the Screen - Problem

Alice has a unique keyboard with only two keys to help her type any target string:

  • Key 1: Appends the character 'a' to the string on screen
  • Key 2: Changes the last character to its next character in the alphabet (e.g., 'c' → 'd', 'z' → 'a')

Starting with an empty screen, Alice wants to type a given target string using the minimum number of key presses.

Your task: Return a list of all intermediate strings that appear on the screen as Alice types, including the final target string, in the order they appear.

Example: To type "abc":

  1. Press Key 1 → "a"
  2. Press Key 1 → "aa"
  3. Press Key 2 → "ab"
  4. Press Key 1 → "aba"
  5. Press Key 2 → "abb"
  6. Press Key 2 → "abc"

Input & Output

example_1.py — Basic Example
$ Input: target = "abc"
Output: ["a", "aa", "ab", "aba", "abb", "abc"]
💡 Note: To build 'abc': add 'a', then add 'a' and transform to 'b', then add 'a' and transform through 'b' to 'c'
example_2.py — Single Character
$ Input: target = "z"
Output: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
💡 Note: Start with 'a' then transform through all letters to reach 'z'
example_3.py — Edge Case
$ Input: target = "a"
Output: ["a"]
💡 Note: Target is 'a', so we only need to press Key 1 once

Constraints

  • 1 ≤ target.length ≤ 1000
  • target consists only of lowercase English letters
  • Key 1 always appends 'a'
  • Key 2 transforms last character to next in alphabet (z → a)

Visualization

Tap to expand
Alice's Magical KeyboardScreen Display"abc"KEY 1+aKEY 2nextStep-by-Step Process:1. Press Key 1 → "a"2. Press Key 1 → "aa"3. Press Key 2 → "ab"4. Press Key 1 → "aba"5. Press Key 2 → "abb"6. Press Key 2 → "abc" ✓
Understanding the Visualization
1
Start Fresh
Alice begins with an empty screen, ready to type her target word
2
Add Foundation
For each position, press the blue button to add an 'a' as the starting point
3
Transform to Target
Press the red button repeatedly to cycle through alphabet until reaching the desired letter
4
Record Progress
Every string that appears on screen is saved in the sequence
Key Takeaway
🎯 Key Insight: The optimal strategy is to append 'a' first, then transform it to the target character, ensuring minimum key presses for each position.
Asked in
Amazon 25 Google 20 Meta 15 Microsoft 12
26.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen