2288. Apply Discount to Prices Medium
1/**
2 * [2288] Apply Discount to Prices
3 *
4 * A sentence is a string of single-space separated words where each word can contain digits, lowercase letters, and the dollar sign '$'. A word represents a price if it is a non-negative real number preceded by a dollar sign.
5 *
6 * For example, "$100", "$23", and "$6.75" represent prices while "100", "$", and "2$3" do not.
7 *
8 * You are given a string sentence representing a sentence and an integer discount. For each word representing a price, apply a discount of discount% on the price and update the word in the sentence. All updated prices should be represented with exactly two decimal places.
9 * Return a string representing the modified sentence.
10 *
11 * Example 1:
12 *
13 * Input: sentence = "there are $1 $2 and 5$ candies in the shop", discount = 50
14 * Output: "there are $0.50 $1.00 and 5$ candies in the shop"
15 * Explanation:
16 * The words which represent prices are "$1" and "$2".
17 * - A 50% discount on "$1" yields "$0.50", so "$1" is replaced by "$0.50".
18 * - A 50% discount on "$2" yields "$1". Since we need to have exactly 2 decimal places after a price, we replace "$2" with "$1.00".
19 *
20 * Example 2:
21 *
22 * Input: sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100
23 * Output: "1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$"
24 * Explanation:
25 * Applying a 100% discount on any price will result in 0.
26 * The words representing prices are "$3", "$5", "$6", and "$9".
27 * Each of them is replaced by "$0.00".
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= sentence.length <= 10^5
33 * sentence consists of lowercase English letters, digits, ' ', and '$'.
34 * sentence does not have leading or trailing spaces.
35 * All words in sentence are separated by a single space.
36 * All prices will be positive integers without leading zeros.
37 * All prices will have at most 10 digits.
38 * 0 <= discount <= 100
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/apply-discount-to-prices/
44// discuss: https://leetcode.com/problems/apply-discount-to-prices/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn discount_prices(sentence: String, discount: i32) -> String {
50 let mut res = sentence.split(' ').fold("".to_owned(), |acc, s| {
51 format!(
52 "{}{} ",
53 acc,
54 if s.chars().next() == Some('$') {
55 if let Ok(price) = &s[1..].parse::<f64>() {
56 // 1e9 is not number in test cases
57 if s.contains('e') {
58 s.to_string()
59 } else {
60 format!("${:.2}", price * (1.0 - (discount as f64) / 100.0))
61 }
62 } else {
63 s.to_string()
64 }
65 } else {
66 s.to_string()
67 }
68 )
69 });
70
71 res.pop();
72 res
73 }
74}
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81
82 #[test]
83 fn test_2288() {
84 assert_eq!(
85 Solution::discount_prices("there are $1 $2 and 5$ candies in the shop".to_owned(), 50),
86 "there are $0.50 $1.00 and 5$ candies in the shop".to_owned()
87 );
88 assert_eq!(
89 Solution::discount_prices("$1e9".to_owned(), 50),
90 "$1e9".to_owned()
91 );
92 }
93}
94
Back
© 2025 bowen.ge All Rights Reserved.