From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- remove-duplicate-letters/Cargo.toml | 8 ++++++++ remove-duplicate-letters/src/main.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 remove-duplicate-letters/Cargo.toml create mode 100644 remove-duplicate-letters/src/main.rs (limited to 'remove-duplicate-letters') diff --git a/remove-duplicate-letters/Cargo.toml b/remove-duplicate-letters/Cargo.toml new file mode 100644 index 0000000..65fc799 --- /dev/null +++ b/remove-duplicate-letters/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "remove-duplicate-letters" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/remove-duplicate-letters/src/main.rs b/remove-duplicate-letters/src/main.rs new file mode 100644 index 0000000..9cc7296 --- /dev/null +++ b/remove-duplicate-letters/src/main.rs @@ -0,0 +1,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 = 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::() + } +} -- cgit v1.2.3