#2390
Medium Algorithms Removing stars from a string
String Stack Simulation
79.0% acceptance
Feb 25, 2026
3207
233
You are given a string s, which contains stars *.
In one operation, you can:
Choose a star in s.
Remove the closest non-star character to its left, as well as remove the star itself.
Return the string after all stars have been removed.
Note:
The input will be generated such that the operation is always possible.
It can be shown that the resulting string will always be unique.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn remove_stars(s: String) -> String {
let mut stack: Vec<char> = vec![];
for c in s.chars() {
if c == '*' { stack.pop(); } else { stack.push(c); }
}
stack.iter().collect()
}
}