Skip to main content
Back to problems
#3324
Medium Algorithms

Find the sequence of strings appeared on the screen

String Simulation
80.2% acceptance
Feb 23, 2026
139
12
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. 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.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn string_sequence(target: String) -> Vec<String> {
    let target = target.as_bytes();
    let mut result = Vec::new();
    let mut current = Vec::new();
    
    for &ch in target {
      // Press key 1: append 'a'
      current.push(b'a');
      result.push(String::from_utf8(current.clone()).unwrap());
      // Press key 2 until we reach ch
      while *current.last().unwrap() < ch {
        *current.last_mut().unwrap() += 1;
        result.push(String::from_utf8(current.clone()).unwrap());
      }
    }
    result
  }
}