java.lang.Object
g2201_2300.s2288_apply_discount_to_prices.Solution

public class Solution extends Object
2288 - Apply Discount to Prices.<p>Medium</p> <p>A <strong>sentence</strong> is a string of single-space separated words where each word can contain digits, lowercase letters, and the dollar sign <code>'$'</code>. A word represents a <strong>price</strong> if it is a sequence of digits preceded by a dollar sign.</p> <ul> <li>For example, <code>&quot;$100&quot;</code>, <code>&quot;$23&quot;</code>, and <code>&quot;$6&quot;</code> represent prices while <code>&quot;100&quot;</code>, <code>&quot;$&quot;</code>, and <code>&quot;$1e5&quot;</code> do not.</li> </ul> <p>You are given a string <code>sentence</code> representing a sentence and an integer <code>discount</code>. For each word representing a price, apply a discount of <code>discount%</code> on the price and <strong>update</strong> the word in the sentence. All updated prices should be represented with <strong>exactly two</strong> decimal places.</p> <p>Return <em>a string representing the modified sentence</em>.</p> <p>Note that all prices will contain <strong>at most</strong> <code>10</code> digits.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;there are $1 $2 and 5$ candies in the shop&rdquo;, discount = 50</p> <p><strong>Output:</strong> &ldquo;there are $0.50 $1.00 and 5$ candies in the shop&rdquo;</p> <p><strong>Explanation:</strong></p> <p>The words which represent prices are &ldquo;$1&rdquo; and &ldquo;$2&rdquo;.</p> <ul> <li> <p>A 50% discount on &ldquo;$1&rdquo; yields &ldquo;$0.50&rdquo;, so &ldquo;$1&rdquo; is replaced by &ldquo;$0.50&rdquo;.</p> </li> <li> <p>A 50% discount on &ldquo;$2&rdquo; yields &ldquo;$1&rdquo;. Since we need to have exactly 2 decimal places after a price, we replace &ldquo;$2&rdquo; with &ldquo;$1.00&rdquo;.</p> </li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;1 2 $3 4 $5 $6 7 8$ $9 $10$&rdquo;, discount = 100</p> <p><strong>Output:</strong> &ldquo;1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$&rdquo;</p> <p><strong>Explanation:</strong></p> <p>Applying a 100% discount on any price will result in 0.</p> <p>The words representing prices are &ldquo;$3&rdquo;, &ldquo;$5&rdquo;, &ldquo;$6&rdquo;, and &ldquo;$9&rdquo;.</p> <p>Each of them is replaced by &ldquo;$0.00&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= sentence.length <= 10<sup>5</sup></code></li> <li><code>sentence</code> consists of lowercase English letters, digits, <code>' '</code>, and <code>'$'</code>.</li> <li><code>sentence</code> does not have leading or trailing spaces.</li> <li>All words in <code>sentence</code> are separated by a single space.</li> <li>All prices will be <strong>positive</strong> numbers without leading zeros.</li> <li>All prices will have <strong>at most</strong> <code>10</code> digits.</li> <li><code>0 <= discount <= 100</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • discountPrices

      public String discountPrices(String sentence, int discount)