Skip to main content

rustgym/leetcode/
_1047_remove_all_adjacent_duplicates_in_string.rs

1struct Solution;
2
3impl Solution {
4    fn remove_duplicates(s: String) -> String {
5        let mut stack: Vec<char> = vec![];
6        for c in s.chars() {
7            if let Some(&top) = stack.last() {
8                if top == c {
9                    stack.pop();
10                } else {
11                    stack.push(c);
12                }
13            } else {
14                stack.push(c)
15            }
16        }
17        stack.iter().collect()
18    }
19}
20
21#[test]
22fn test() {
23    let s = "abbaca".to_string();
24    let t = "ca".to_string();
25    assert_eq!(Solution::remove_duplicates(s), t);
26}