summaryrefslogtreecommitdiff
path: root/number-of-different-integers-in-a-string/src/main.rs
blob: 5a8256e3e4d78e473a488b5ac83bef037606d2e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
    }
}