Find the Sequence of Strings Appeared on the Screen - Problem

You are given a string target. Alice is going to type target on her computer using a special keyboard that has only two keys:

  • Key 1 appends the character "a" to the string on the screen.
  • Key 2 changes the last character of the string on the screen to its next character in the English alphabet. For example, "c" changes to "d" and "z" changes to "a".

Note that initially there is an empty string "" on the screen, so she can only press Key 1 first.

Return a list of all strings that appear on the screen as Alice types target, in the order they appear, using the minimum key presses.

Input & Output

Example 1 — Basic Case
$ Input: target = "abc"
Output: ["a","ab","aba","abb","abc"]
💡 Note: Start empty → add 'a' → add 'a' to get 'aa' then cycle to 'ab' → add 'a' to get 'aba', cycle to 'abb', then 'abc'
Example 2 — Single Character
$ Input: target = "a"
Output: ["a"]
💡 Note: Only need to add 'a' once, no cycling required
Example 3 — Cycling Required
$ Input: target = "za"
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","za"]
💡 Note: First position cycles through entire alphabet to reach 'z', then add 'a'

Constraints

  • 1 ≤ target.length ≤ 1000
  • target consists of lowercase English letters only

Visualization

Tap to expand
String Sequence Generation for 'bc'target"bc"Key OperationsKey 1: Add 'a'Key 2: Cycle alphabetGenerated Sequence["a", "b", "ba", "bb", "bc"]Step-by-Step Process1. Start: "" → Key 1 → "a"2. Position 1: "a" → Key 2 → "b"3. Extend: "b" → Key 1 → "ba"4. Position 2: "ba" → Key 2 → "bb" → Key 2 → "bc"Result: 5 strings showing all intermediate steps
Understanding the Visualization
1
Input
Target string 'bc' to be typed
2
Process
Use Key 1 (add 'a') and Key 2 (cycle alphabet) to build string
3
Output
All intermediate strings in order
Key Takeaway
🎯 Key Insight: Simulate the exact typing process by extending with 'a' then cycling to target character
Asked in
Google 23 Microsoft 15
12.5K Views
Medium Frequency
~15 min Avg. Time
234 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