1
impl Solution {
2
pub fn original_digits(s: String) -> String {
3
if s.is_empty() {
4
return String::new();
5
}
6

7
let ind_let = |x: u8| -> usize { (x - b'a') as usize };
8
let str_to_arr = |s: &str| -> [usize; 26] {
9
s.as_bytes().iter().fold([0; 26], |mut acc, x| {
10
acc[ind_let(*x)] += 1;
11
acc
12
})
13
};
14

15
let num_str = [
16
(str_to_arr("zero"), ind_let(b'z'), '0'),
17
(str_to_arr("two"), ind_let(b'w'), '2'),
18
(str_to_arr("four"), ind_let(b'u'), '4'),
19
(str_to_arr("six"), ind_let(b'x'), '6'),
20
(str_to_arr("eight"), ind_let(b'g'), '8'),
21
(str_to_arr("three"), ind_let(b'r'), '3'),
22
(str_to_arr("five"), ind_let(b'f'), '5'),
23
(str_to_arr("seven"), ind_let(b's'), '7'),
24
(str_to_arr("nine"), ind_let(b'i'), '9'),
25
(str_to_arr("one"), ind_let(b'o'), '1'),
26
];
27

28
let mut mas_w = str_to_arr(&s);
29
let mut res = vec![];
30

31
for &(mas, ind, c) in num_str.iter() {
32
let k = mas_w[ind] / mas[ind];
33
if k == 0 {
34
continue;
35
}
36

37
res.append(&mut vec![c; k]);
38

39
for i in 0..26 {
40
if mas[i] > 0 {
41
mas_w[i] -= k * mas[i]
42
}
43
}
44
}
45

46
res.sort_unstable();
47
res.iter().collect::<String>()
48
}
49
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0