LeetCode solutions

2373. Apply Discount to Prices

My accepted Python solution to LeetCode problem 2373, Apply Discount to Prices, running in 54ms.

  • Difficulty: Medium
  • Python
  • Runtime 54ms
  • Memory 19.8MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 54ms, memory 19.8MB, accepted 2026-01-02.

python
class Solution:
    def discountPrices(self, sentence: str, discount: int) -> str:
        words = sentence.split()
        result = []
        for word in words:
            if word.startswith('$') and len(word) > 1 and word[1:].isdigit():
                price = int(word[1:])
                discounted = price * (100 - discount) / 100
                result.append('$' + '{:.2f}'.format(discounted))
            else:
                result.append(word)
        return ' '.join(result)

Source