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.
"$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 = 50 Output: "there are $0.50 $1.00 and 5$ candies in the shop" Explanation: The words which represent prices are "$1" and "$2". - A 50% discount on "$1" yields "$0.50", so "$1" is replaced by "$0.50". - 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".
Example 2:
Input: sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100 Output: "1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$" Explanation: Applying a 100% discount on any price will result in 0. The words representing prices are "$3", "$5", "$6", and "$9". Each of them is replaced by "$0.00".
Constraints:
1 <= sentence.length <= 105sentence consists of lowercase English letters, digits, ' ', and '$'.sentence does not have leading or trailing spaces.sentence are separated by a single space.10 digits.0 <= discount <= 100This approach involves iterating through each word in the sentence, checking if it represents a price, and then applying the discount if it does.
Here's a step-by-step breakdown:
The function 'apply_discount' takes a sentence and a discount as input. It splits the sentence into words, checks each word to see if it's a price, applies the discount, and replaces the price in the list with the updated value. Finally, it joins the words back into a sentence.
JavaScript
Time Complexity: O(n) - where n is the length of the sentence since we iterate over each character once.
Space Complexity: O(n) - for storing words in the list.
This approach uses regular expressions to identify price patterns within the sentence.
Steps:
The C++ solution uses std::regex to find instances of prices, calculates the new price using the discount, formats it to two decimal places, and builds a new string with the updated prices.
Java
Time Complexity: O(n * m) - where n is the length of the sentence and m is the approximate number of price matches.
Space Complexity: O(n) - for the result string copy.
| Approach | Complexity |
|---|---|
| Iterative Approach Using Split and Join | Time Complexity: O(n) - where n is the length of the sentence since we iterate over each character once. |
| Regex and String Manipulation | Time Complexity: O(n * m) - where n is the length of the sentence and m is the approximate number of price matches. |
Final Prices With a Special Discount in a Shop - Leetcode 1475 - Python • NeetCodeIO • 5,547 views views
Watch 9 more video solutions →Practice Apply Discount to Prices with our built-in code editor and test cases.
Practice on FleetCode