#1047
Easy Algorithms Remove all adjacent duplicates in string
String Stack
72.8% acceptance
Feb 25, 2026
7061
275
You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn remove_duplicates(s: String) -> String {
let mut stack: Vec<char> = vec![];
for c in s.chars() {
if stack.last() == Some(&c) { stack.pop(); }
else { stack.push(c); }
}
stack.into_iter().collect()
}
}