Discount Only Complete Dollar-Price Words in a Sentence

Quick Overview

Apply an integer percentage discount to complete dollar-price words in a sentence and format each updated price with exactly two decimal places.

Discount Only Complete Dollar-Price Words in a Sentence

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Words in `sentence` are separated by one space. A price word is a dollar sign followed only by decimal digits, with at least one digit. Apply discount percent to each such word and write its new value with exactly two decimal places. Preserve every other word and the spaces. ```cpp class Solution { public: string discountPrices(string sentence, int discount); }; ``` ### Examples **Example 1:** ```text Input: sentence = "pay $25 or 25$", discount = 20 Output: "pay $20.00 or 25$" Explanation: Only "$25" is a price. Reducing it by 20 percent gives "$20.00"; "25$" remains unchanged. ``` **Example 2:** ```text Input: sentence = "$3 $ abc$", discount = 100 Output: "$0.00 $ abc$" Explanation: The valid price becomes "$0.00". The lone dollar sign and "abc$" are not prices. ``` ### Constraints - `1 <= sentence.length <= 100000`. - Characters are lowercase English letters, digits, spaces, and$. - There are no leading/trailing spaces and exactly one space between words. - Prices are positive, have no leading zeroes, and contain at most 10 digits. - `0 <= discount <= 100`. ### Clarifying Questions - Is a lone `$` a price? No; at least one digit must follow it. - Is `$1e5` a price? No; every character after the initial dollar sign must be a digit. - Must trailing zeroes be written? Yes; every updated price has exactly two decimal places. - What happens at discounts of zero or 100 percent? The value stays the same or becomes zero, respectively, while still using two decimal places. ```hint Check the boundary Check the whole word, including any characters after its digits. ```

Overview: Apply an integer percentage discount to complete dollar-price words in a sentence and format each updated price with exactly two decimal places.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Words in sentence are separated by one space. A price word is a dollar sign followed only by decimal digits, with at least one digit. Apply discount percent to each such word and write its new value with exactly two decimal places. Preserve every other word and the spaces.

class Solution {
public:
    string discountPrices(string sentence, int discount);
};

Examples

Example 1:

Input: sentence = "pay $25 or 25$", discount = 20
Output: "pay $20.00 or 25$"
Explanation: Only "$25" is a price. Reducing it by 20 percent gives "$20.00"; "25$" remains unchanged.

Example 2:

Input: sentence = "$3 $ abc$", discount = 100
Output: "$0.00 $ abc$"
Explanation: The valid price becomes "$0.00". The lone dollar sign and "abc$" are not prices.

Constraints

  • 1 <= sentence.length <= 100000 .
  • Characters are lowercase English letters, digits, spaces, and$.
  • There are no leading/trailing spaces and exactly one space between words.
  • Prices are positive, have no leading zeroes, and contain at most 10 digits.
  • 0 <= discount <= 100 .

Clarifying Questions Guidance

  • Is a lone $ a price? No; at least one digit must follow it.
  • Is $1e5 a price? No; every character after the initial dollar sign must be a digit.
  • Must trailing zeroes be written? Yes; every updated price has exactly two decimal places.
  • What happens at discounts of zero or 100 percent? The value stays the same or becomes zero, respectively, while still using two decimal places.