Leetcode Problem 2288. Apply Discount to Prices

2288. Apply Discount to Prices

Leetcode Solutions

Iterate and Replace Prices with Discount

  1. Split the sentence into words based on spaces.
  2. Iterate through each word in the list of words.
  3. For each word, check if it starts with a dollar sign and is followed by digits only.
  4. If the word represents a price, remove the dollar sign and parse the remaining part as a number.
  5. Apply the discount to the number to get the discounted price.
  6. Format the discounted price to two decimal places and prepend the dollar sign.
  7. Replace the original word with the formatted discounted price.
  8. Join the modified list of words back into a sentence.
  9. Return the modified sentence.
UML Thumbnail

Regex Matching and String Formatting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...