aboutsummaryrefslogtreecommitdiff
path: root/challenge-340/benjamin-andre/rust/ch-1.rs
blob: 235095ac87da2b296c71eddc2a8ec6f39ca81c4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/sh
//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit

fn duplicate_removals(s: &str) -> String {
    let mut stack = Vec::new();
    for c in s.chars() {
        if stack.last() == Some(&c) {
            stack.pop();
        } else {
            stack.push(c);
        }
    }
    stack.into_iter().collect()
}

#[test]
fn example() {
    assert_eq!(duplicate_removals("abbaca"), "ca");
    assert_eq!(duplicate_removals("azxxzy"), "ay");
    assert_eq!(duplicate_removals("aaaaaaaa"), "");
    assert_eq!(duplicate_removals("aabccba"), "a");
    assert_eq!(duplicate_removals("abcddcba"), "");
}