aboutsummaryrefslogtreecommitdiff
path: root/make_string_great.rs
blob: 76a28a2acfdd3d0c5f3ed1d48a33d50286ef5d0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Solution;

impl Solution {
    pub fn make_good(s: String) -> String {
        let diff_cases = |c1: char, c2: char| c1.to_ascii_lowercase() == c2.to_ascii_lowercase() && c1 != c2;
        s.chars().into_iter().fold(Vec::with_capacity(s.len()), |mut v: Vec<char>, c| {
            match v.last() {
                Some(prev_c) if diff_cases(c, *prev_c) => {
                    v.pop();
                }
                _ => v.push(c)
            }
            v
        }).iter().collect()
    }
}

fn main() {
    let s = "leEeetcode".to_string();
    print!("{}", Solution::make_good(s));
}