Apply Discount to Prices LeetCode solution in Rust

1
use std::fmt::Write as FmtWrite;
2

3
impl Solution {
4
pub fn discount_prices(sentence: String, discount: i32) -> String {
5
let mut new_str = String::new();
6

7
for (i, word) in sentence.split_whitespace().enumerate() {
8
if i != 0 {
9
new_str.push(' ');
10
}
11
if word.len() > 0 {
12
if word.as_bytes()[0] == b'$' {
13
if (&word[1..]).chars().all(|c| c >= '0' && c <= '9') {
14
if let Ok(num) = &word[1..].parse::<f64>() {
15
let n = num * ((100 - discount) as f64 / 100_f64);
16

17
write!(&mut new_str, "${:.2}", n);
18
continue;
19
}
20
}
21
}
22
}
23

24
new_str.push_str(word);
25
}
26

27
new_str
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0