diff options
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 + } +} |