1805. Number of Different Integers in a String

You are given a string `word` that consists of digits and lowercase English letters.

You will replace every non-digit character with a space. For example, `"a123bc34d8ef34"` will become `" 123  34 8  34"`. Notice that you are left with some integers that are separated by at least one space: `"123"`, `"34"`, `"8"`, and `"34"`.

Return the number of different integers after performing the replacement operations on `word`.

Two integers are considered different if their decimal representations without any leading zeros are different.

Example 1:

```Input: word = "a123bc34d8ef34"
Output: 3
Explanation: The three different integers are "123", "34", and "8". Notice that "34" is only counted once.
```

Example 2:

```Input: word = "leet1234code234"
Output: 2
```

Example 3:

```Input: word = "a1b01c001"
Output: 1
Explanation: The three integers "1", "01", and "001" all represent the same integer because
the leading zeros are ignored when comparing their decimal values.
```

Constraints:

• `1 <= word.length <= 1000`
• `word` consists of digits and lowercase English letters.

1805. Number of Different Integers in a String
``````struct Solution;

use std::collections::HashSet;

impl Solution {
fn num_different_integers(word: String) -> i32 {
let word: String = word
.chars()
.map(|c| if c.is_numeric() { c } else { ' ' })
.collect();
let mut hs: HashSet<String> = HashSet::new();
let words: Vec<Vec<char>> = word
.split_whitespace()
.map(|s| s.chars().rev().collect())
.collect();
for w in words {
let mut wc = w.clone();
while let Some(c) = wc.pop() {
if c == '0' && w.last() == Some(&'0') {
continue;
} else {
if c != '0' {
wc.push(c);
}
break;
}
}
let s: String = wc.iter().collect();
hs.insert(s);
}
hs.len() as i32
}
}

#[test]
fn test() {
let word = "a123bc34d8ef34".to_string();
let res = 3;
assert_eq!(Solution::num_different_integers(word), res);
let word = "leet1234code234".to_string();
let res = 2;
assert_eq!(Solution::num_different_integers(word), res);
let word = "a1b01c001".to_string();
let res = 1;
assert_eq!(Solution::num_different_integers(word), res);
}
``````