All problems
0855MediumString

Marking Down the Priced Words

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2288Apply Discount to Prices

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A notice reads sentence, a run of words separated by single spaces with no space at either end.

A word is a price when it begins with '$' and every character after that is a digit, and there is at least one such digit. Any other word is left alone, including a bare "$" and anything with a non-digit after the '$'.

Every price is reduced by discount percent. Write the reduced amount with exactly two digits after a decimal point, keeping the leading '$'. Taking a whole number of percent off a whole amount always lands on an exact number of hundredths, so two digits is always enough and nothing has to be rounded.

Return the notice after every price has been marked down.

Examples

Example 1

Input
sentence = "there are $1 $2 and 5$ items", discount = 50
Output
"there are $0.50 $1.00 and 5$ items"

The words "$1" and "$2" are prices and halve to "$0.50" and "$1.00". The word "5$" does not begin with the marker, so it is left alone.

Example 2

Input
sentence = "$3", discount = 33
Output
"$2.01"

Taking 33 percent off 3 leaves 2.01, which written to two digits is "$2.01".

Example 3

Input
sentence = "$", discount = 25
Output
"$"

The word is the marker with no digits after it, so it is not a price and is left unchanged.

Constraints

  • 1 <= sentence.length <= 10^5
  • sentence consists of lowercase English letters, digits, spaces and '$' only
  • sentence has no leading or trailing space, and words are separated by single spaces
  • Every price holds at most 10 digits and no leading zero
  • 0 <= discount <= 100

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def mark_down_prices(sentence: str, discount: int) -> str:
Java
public String markDownPrices(String sentence, int discount)
September 7
Apply