Mid/SeniorString
Apply Discount to Prices
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 sequence of digits preceded by a dollar sign.
- For example,
"$100","$23", and"$6"represent prices while"100","$", and"$1e5"do not.
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.
Return a string representing the modified sentence.
Note that all prices will contain at most 10 digits.
Example 1
Input
sentence = "there are $1 $2 and 5$ candies in the shop", discount = 50Output
"there are $0.50 $1.00 and 5$ candies in the shop"The words which represent prices are "$1" and "$2"; applying a 50% discount replaces them with "$0.50" and "$1.00" respectively.
Example 2
Input
sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100Output
"1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$"Applying a 100% discount turns each valid price word, "$3", "$5", "$6", and "$9", into "$0.00".
Constraints
- 1 <= sentence.length <= 10^5
- sentence consists of lowercase English letters, digits, ' ', and '$'.
- sentence does not have leading or trailing spaces.
- All words in sentence are separated by a single space.
- All prices will be positive numbers without leading zeros.
- All prices will have at most 10 digits.
- 0 <= discount <= 100