summaryrefslogtreecommitdiff
path: root/remove-duplicate-letters/src/main.rs
blob: 9cc729605e394ce0d7164c9e4e3fe45e27d0570b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    println!("Hello, world!");
    let tests = [
        ("bcabc", "abc"),
        ("cbacdcbc", "acdb")
    ];

    for test in tests {
        println!("{:?} is {:?} should be {:?}", test.0,
                 Solution::remove_duplicate_letters(test.0.to_string()), test.1);
    }
}

struct Solution;

impl Solution {
    pub fn remove_duplicate_letters(s: String) -> String {
        let mut a: Vec<char> = Vec::with_capacity(s.len());
        let start = s.chars().enumerate().reduce(|a,b| if a.1 < b.1 {a} else {b});
        for c in s.chars().skip(start.unwrap().0) {
            if a.last().is_none() || c > *a.last().unwrap() {
                a.push(c);
            }
        }
        a.iter().collect::<String>()
    }
}