Skip to main content
Back to problems
#3612
Medium Algorithms

Process string with special operations i

String Simulation
65.0% acceptance
Feb 25, 2026
56
11
You are given a string s consisting of lowercase English letters and the special characters: *, #, and %. Build a new string result by processing s according to the following rules from left to right: If the letter is a lowercase English letter append it to result. A '*' removes the last character from result, if it exists. A '#' duplicates the current result and appends it to itself. A '%' reverses the current result. Return the final string result after processing all characters in s.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn process_str(s: String) -> String {
    let mut result: Vec<char> = Vec::new();
    for ch in s.chars() {
      match ch {
        '*' => { result.pop(); }
        '#' => {
          let dup = result.clone();
          result.extend(dup);
        }
        '%' => { result.reverse(); }
        c   => { result.push(c); }
      }
    }
    result.iter().collect()
  }
}