From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- .../Cargo.toml | 8 ++++++ .../src/main.rs | 29 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 number-of-different-integers-in-a-string/Cargo.toml create mode 100644 number-of-different-integers-in-a-string/src/main.rs (limited to 'number-of-different-integers-in-a-string') diff --git a/number-of-different-integers-in-a-string/Cargo.toml b/number-of-different-integers-in-a-string/Cargo.toml new file mode 100644 index 0000000..aca88ce --- /dev/null +++ b/number-of-different-integers-in-a-string/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "number-of-different-integers-in-a-string" +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/number-of-different-integers-in-a-string/src/main.rs b/number-of-different-integers-in-a-string/src/main.rs new file mode 100644 index 0000000..5a8256e --- /dev/null +++ b/number-of-different-integers-in-a-string/src/main.rs @@ -0,0 +1,29 @@ +fn main() { + println!("Hello, world!"); + let tests = [ + ("a123bc34d8ef34", 3), + ("leet1234code234", 2), + ("a1b01c001", 1), + ("035985750011523523129774573439111590559325a1554234973", 2) + ]; + for test in tests { + println!("{:?} is {} should be {}", test.0, + Solution::num_different_integers(test.0.to_string()), test.1); + } +} + +struct Solution; + +use std::collections::HashSet; + +impl Solution { + pub fn num_different_integers(word: String) -> i32 { + let result = unsafe { word.as_bytes() }.split(u8::is_ascii_alphabetic) + .filter(|x| !x.is_empty()) + .map(|mut x| {while x.starts_with(b"0") {x = x.strip_prefix(b"0").unwrap(); } x}) + // .map(|x| x.iter().rev().enumerate().fold(0, |acc, (i,&v)| acc + (v as u32 -48) * 10_u32.pow(i as u32))) + .collect::>(); + println!("{result:?}"); + result.len() as i32 + } +} -- cgit v1.2.3