LeetCode 1047. Remove All Adjacent Duplicates In String

Problem Statement


impl Solution {
pub fn remove_duplicates(s: String) -> String {
let mut stack = vec![];
let mut ans = "".to_string();
for c in s.chars() {
if stack.is_empty() || c != *stack.get(stack.len() - 1).unwrap() {
stack.push(c); continue;
}
if c == *stack.get(stack.len() - 1).unwrap() {
stack.pop();
}
}
while !stack.is_empty() {
ans.push(stack.pop().unwrap());
}
return ans.chars().rev().collect::<String>();
}
}