diff options
author | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
commit | 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb (patch) | |
tree | 0072cca328fe5adb2ed61004010228ff85e2164d /number-of-different-integers-in-a-string/src/main.rs |
Diffstat (limited to 'number-of-different-integers-in-a-string/src/main.rs')
-rw-r--r-- | number-of-different-integers-in-a-string/src/main.rs | 29 |
1 files changed, 29 insertions, 0 deletions
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::<HashSet<&[u8]>>(); + println!("{result:?}"); + result.len() as i32 + } +} |