#2810
Easy Algorithms Faulty keyboard
String Simulation
80.0% acceptance
Feb 25, 2026
506
16
Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected.
You are given a 0-indexed string s, and you type each character of s using your faulty keyboard.
Return the final string that will be present on your laptop screen.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn final_string(s: String) -> String {
use std::collections::VecDeque;
let mut dq: VecDeque<char> = VecDeque::new();
let mut reversed = false;
for c in s.chars() {
if c == 'i' {
reversed = !reversed;
} else if reversed {
dq.push_front(c);
} else {
dq.push_back(c);
}
}
if reversed { dq.iter().rev().collect() } else { dq.iter().collect() }
}
}